admin管理员组文章数量:1379578
I have made a custom directive and used ng-model, but when the model updates, the directive not even though i'm watching the event. Here's the code:
angular.module('Directives').directive("customDirective", ['$window', function ($window) {
return {
restrict: "E",
require: 'ngModel',
scope: {
ngModel: '=',
},
link: function (scope, elem, attrs, ngModel) {
// IF the model changes, re-render
scope.$watch('ngModel', function (newVal, oldVal) {
render();
});
// We want to re-render in case of resize
angular.element($window).bind('resize', function () {
//this does work
render();
})
var render = function () {
//doing some work here
};
}
}}]);
and the view:
<div ng-repeat="product in pageCtrl.productList track by product.id">
<h3>{{ product.name }}</h3>
<custom-directive ng-model="product.recentPriceHistory">
</custom-directive>
Whenever new values are added to a products recentPriceHistory, the view doesnt get updated.
I have made a custom directive and used ng-model, but when the model updates, the directive not even though i'm watching the event. Here's the code:
angular.module('Directives').directive("customDirective", ['$window', function ($window) {
return {
restrict: "E",
require: 'ngModel',
scope: {
ngModel: '=',
},
link: function (scope, elem, attrs, ngModel) {
// IF the model changes, re-render
scope.$watch('ngModel', function (newVal, oldVal) {
render();
});
// We want to re-render in case of resize
angular.element($window).bind('resize', function () {
//this does work
render();
})
var render = function () {
//doing some work here
};
}
}}]);
and the view:
<div ng-repeat="product in pageCtrl.productList track by product.id">
<h3>{{ product.name }}</h3>
<custom-directive ng-model="product.recentPriceHistory">
</custom-directive>
Whenever new values are added to a products recentPriceHistory, the view doesnt get updated.
Share Improve this question asked Jun 16, 2015 at 8:49 FerencFerenc 552 silver badges5 bronze badges1 Answer
Reset to default 7By default when paring an old value with new value angular will be checked for "reference" equality. But if you need to check for the value then you need to do like this,
scope.$watch('ngModel', function (newVal, oldVal) {
render();
}, true);
But the problem here is that angular will deep watch all the properties of the ngModel for changes, If the ngModel
variable is a plex object it wilt affects the performance. What you can do it to avoid this is to check only for one property,
scope.$watch('ngModel.someProperty', function (newVal, oldVal) {
render();
}, true);
本文标签: javascriptscopewatch not working in angular directiveStack Overflow
版权声明:本文标题:javascript - scope.$watch not working in angular directive - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744454713a2606933.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论