我制定了一个指令,包装一个jQuery插件:
angular.module('ngJQueryPlugin', [])
.controller('MainCtrl', function ($scope) {
$scope.input = {'hexa': 0};
})
.directive('jQueryPlugin', function ($compile, $parse) {
return {
restrict: 'E',
require: '?ngModel',
link: function ($scope, $element, $attributes, ngModel) {
if (ngModel === null) return;
$scope.$watch($attributes.ngModel, function (hexaFromModel) {
$element.data('jQueryPlugin').updateJQueryPluginUI(hexaFromModel);
}, true);
$element.jQueryPluginConstructor({}, function (hexaFromjQueryPlugin) {
$scope.$apply(function () {
ngModel.$setViewValue({ hexa: hexaFromjQueryPlugin});
ngModel.$render();
});
});
}
};
});指令实例化:
<jquery-plugin ng-model="input" />
<input type="text" ng-model="input.hexa"/>指令中的范围监视跟踪每个模型更新并通知jQueryPlugin。
jQueryPlugin构造函数中的第二个参数是一个回调函数,它在每次jQueryPlugin UI更新十六进制颜色时调用。然后它在作用域上设置新值。
我的问题是,$setViewValue导致调用$watch。$watch更新回jQueryPlugin,这是无用的,因为我们最初从jQueryPlugin回调中得到通知。
我想过的一个解决方案是在作用域中使用标志,以了解更新最初是在jQueryPlugin还是在作用域中启动的。这将防止递归调用。
还有别的办法吗?例如,防止$setViewValue触发$watch,即使模型已经更新?
更新:我刚刚创建了一个柱塞。http://plnkr.co/edit/avumn9MJwdjmxHNtrmSF?p=preview --如果我在第一个输入中输入文本(从角度),就会通知jQuery插件:正常行为。如果我在来自jQuery组件的第二个输入中输入文本,则不应该向它发出通知。
发布于 2014-08-05 03:09:11
$scope.$watch($attributes.ngModel, function (hexaFromModel, oldValue) {
if (hexaFromModel === oldValue) {
return;
}
$element.data('jQueryPlugin').updateJQueryPluginUI(hexaFromModel);
});请注意,这并不会阻止表被调用,但是如果没有更改,则停止运行监视逻辑。
发布于 2014-08-06 06:43:21
检测来自ngModel.$setViewValue()的更改的一种方法是与ngModel.$modelValue进行比较。你可以这样做:
$scope.$watch($attributes.ngModel, function (hexaFromModel) {
if (angular.equals(hexaFromModel, ngModel.$modelValue)) {
return;
}
$element.data('jQueryPlugin').updateJQueryPluginUI(hexaFromModel);
}, true);或者您也可以使用$formatters,而不是像这样观看$attributes.ngModel:
ngModel.$formatters.push(function (value) {
$element.data('jQueryPlugin').updateJQueryPluginUI(value);
});希望这能有所帮助。
编辑:--原来hexaFromModel实际上是一个对象,而不是一个简单的字符串,这使得上面的解决方案毫无用处。
在这种情况下,如果更新来自插件,我想不出比设置一个标志来绕过监视监听器更好的解决方案了。如下所示:
var updatingByPlugin = false;
$scope.$watch($attributes.ngModel, function(newValue, oldValue) {
if (updatingByPlugin) {
return;
}
jQueryPlugin.update(newValue);
}, true);
jQueryPlugin = new jQueryPluginConstructor($element, {}, function(hexFromjQueryPlugin) {
console.log('notified');
updatingByPlugin = true;
$scope.$apply(function () {
ngModel.$setViewValue({
hex: hexFromjQueryPlugin
});
});
updatingByPlugin = false;
});发布于 2016-01-26 17:31:13
您应该编写一个$render函数,而不是调用它:
angular.module('ngJQueryPlugin', [])
.controller('MainCtrl', function ($scope) {
$scope.input = {'hexa': 0};
})
.directive('jQueryPlugin', function ($compile, $parse) {
return {
restrict: 'E',
require: '?ngModel',
link: function ($scope, $element, $attributes, ngModel) {
if (ngModel === null) return;
ngModel.$render = function () {
if(ngModel.$viewValue)
$element.data('jQueryPlugin').updateJQueryPluginUI(ngModel.$viewValue);
}
$element.jQueryPluginConstructor({}, function (hexaFromjQueryPlugin) {
$scope.$apply(function () {
ngModel.$setViewValue({ hexa: hexaFromjQueryPlugin});
});
});
}
};
});https://stackoverflow.com/questions/25130565
复制相似问题