使用$watch更改html内容
using $watch for change in html content
如何使用$watch更改div的html内容。我尝试过使用示例,但它不起作用。它是第一次手表,只要有变化,手表就不会被触发。请在下面找到代码
html
<body ng-app="myApp">
<div editable-content></div>
</body>
角度
var myApp = angular.module('myApp',[]);
myApp.directive('editableContent', function(){
return {
replace: true,
template:'<div contentEditable style="border:1px solid;width:300px;height:40px">this is
a div</div>',
link: function(scope, element, attr){
scope.$watch(function()
{return element.html()}, function(value){
console.log(value);
});
}
};
});
我建议您应该在指令中使用独立的范围,为内容设置一个变量,并且您可以很容易地观察到这个变量的变化。
html
<body ng-app="myApp">
<div editable-content></div>
</body>
js
var myApp = angular.module('myApp',[]);
myApp.directive('editableContent', function(){
return {
replace: true,
scope:{
content: '='
}
template:'<div contentEditable content='content' style="border:1px solid;width:300px;height:40px">this is
a div</div>',
link: function(scope, element, attr){
scope.$watch(function()
{scope.content}, function(value){
console.log(value);
});
}
};
});
相关文章:
- 如何根据html内容使用ng类-AngularJS
- JavaScript在其他页面上用html内容填充文本框
- Rails:如何在浏览器中显示动态html内容,而不会对其进行转义,也不会引起XSS攻击
- 使用javascript动态创建html内容/元素
- 无法将大型html内容POST到Chrome上的服务器
- 使用li元素的html内容更改该元素的背景
- 如何从ajax返回两个html内容
- jQuery获取子DIV的HTML内容
- HTML内容下推到固定背景图像下方
- 在angular中,使用ng repeat生成动态html内容
- 使用$watch更改html内容
- 如何使用window.showModalDialog()显示HTML内容
- 如何在使用 UIWebView 编辑 HTML 内容时正确调整输入语言更改 (LTR/RTL) 的文本对齐方式
- 获取垂直溢出容器的 html 内容
- 为数组中的每个项目追加 html 内容
- Dojo:在 iframe 加载时解析 HTML 内容
- 允许 Itemp 在 Enyo 中使用 HTML 内容
- 从选项卡中获取 HTML 内容
- 可以将HTML内容放在画布上(登录页)
- 只有当类是硬编码的时,才可以使用jQuery来显示/隐藏html内容