admin管理员组文章数量:1427478
Here is my problem. For example, we have the following directive, which uses some jQuery widget behind the scenes :
module.directive('myWidget', [function() {
return {
require: "ngModel",
restrict: "A",
replace: true,
templateUrl: "templates/myWidget.html",
link: function(scope, element, attrs, ctrl) {
element.widget_name().on('value_updated', function(event) {
scope.$apply(function() {
var newModelValue = event.some_value;
ctrl.$setViewValue(newModelValue);
});
});
scope.$watch(attrs["ngModel"], function(value){
element.widget_name('set_value', value);
});
}
};
}]);
So, if model's value changes, then the handler which is registered using $watch to listen for changes in model will be executed, and, consequently, widget's 'set_value' method will be executed too. This means that 'value_updated' event will be triggered.
My question is: what is the best practice to implement similar behavior in directives to avoid extra calls of DOM event handlers and watchers?
Here is my problem. For example, we have the following directive, which uses some jQuery widget behind the scenes :
module.directive('myWidget', [function() {
return {
require: "ngModel",
restrict: "A",
replace: true,
templateUrl: "templates/myWidget.html",
link: function(scope, element, attrs, ctrl) {
element.widget_name().on('value_updated', function(event) {
scope.$apply(function() {
var newModelValue = event.some_value;
ctrl.$setViewValue(newModelValue);
});
});
scope.$watch(attrs["ngModel"], function(value){
element.widget_name('set_value', value);
});
}
};
}]);
So, if model's value changes, then the handler which is registered using $watch to listen for changes in model will be executed, and, consequently, widget's 'set_value' method will be executed too. This means that 'value_updated' event will be triggered.
My question is: what is the best practice to implement similar behavior in directives to avoid extra calls of DOM event handlers and watchers?
Share Improve this question edited Mar 13, 2013 at 19:23 Mark Rajcok 365k114 gold badges500 silver badges494 bronze badges asked Mar 13, 2013 at 18:21 oaleynikoaleynik 6855 silver badges19 bronze badges1 Answer
Reset to default 4Instead of scope.$watch()
, I suggest implementing ctrl.$render()
. $render should only be called if something inside Angular changes the model. Fiddle example.
This solves a problem you did not mention. Unfortunately, it does not solve the problem you did mention. In the fiddle, a blur
event is bound, rather than some widget.on() event. Maybe that would work for you – i.e., only update the model on blur, rather than every keystroke (this assumes your widget is accepting keystrokes, however).
Maybe you could also ask the widget author to provide a "set" method that does not trigger an event. Then that could be used in the $render() method.
本文标签: javascriptAngularJS directivesbest practices when using ngModel with jQuery widgetStack Overflow
版权声明:本文标题:javascript - AngularJS directives - best practices when using ngModel with jQuery widget - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745501707a2661062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论