Pra*_*ruz 5 javascript jquery scope watch angularjs
我有一个包含所有样式控件的富文本框.
当我在其中输入新文本时 - 它会保存.
当我对内容(文本)进行任何更改以及一些样式(如颜色突出显示和粗体文本)时,它会保存更改的文本和样式.
但是,当我只是在没有任何内容更改的情况下进行样式更改时 - 它不会保存这些样式更改.
我使用$ watch来比较新值和旧值.
如何使其适用于风格变化?
moh*_*ias 10
angular.module("app",[]).directive('spyStyle', [function () {
return {
link: function (scope, element, attrs) {
scope.$watch(function () {
return element.css(attrs['spyAttribute']);
}, styleChangedCallBack,
true);
function styleChangedCallBack(newValue, oldValue) {
if (newValue !== oldValue) {
// do something interesting here
}
}
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
在您的HTML中:
<div spy-style spy-attribute="height" >
Run Code Online (Sandbox Code Playgroud)
如果您想在指令外执行自定义函数:
<div spy-style="functionNameToExecute" spy-attribute="height" >
Run Code Online (Sandbox Code Playgroud)
在您的指令代码中进行此更改:
angular.module("app",[]).directive('spyStyle', [function () {
return {
link: function (scope, element, attrs) {
scope.$watch(function () {
return element.css(attrs['spyAttribute']);
}, styleChangedCallBack,
true);
function styleChangedCallBack(newValue, oldValue) {
if (newValue !== oldValue) {
// do something interesting here
// invoking callback function:
scope[attrs['spyStyle']](newValue);
}
}
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7012 次 |
| 最近记录: |