Angularjs:听指令中的模型变化 [英] Angularjs: Listen to model change in a directive

查看:24
本文介绍了Angularjs:听指令中的模型变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出在指令中更新模型时如何监听.

eventEditor.directive('myAmount',function(){返回 {限制:'A',链接:功能(范围,元素,属性){scope.$watch(attr['ngModel'], 函数 (v) {console.log('值改变了,新值是:' + v);});}}}};

});

该指令在 ng-repeat 中被调用为

很高兴得到任何帮助.我不明白 ng-repeat 中的 scope 属性是怎样的.

谢谢.

解决方案

http://jsbin.com/mihupo/1/edit

attrs 代替 attr

app.directive('myAmount',function(){返回 {限制:'A',链接:功能(范围,元素,属性){scope.$watch(attrs['ngModel'], 函数 (v) {console.log('值改变了,新值是:' + v);});}};});

I'm trying to find out how I can listen to when the model is updated within an directive.

eventEditor.directive('myAmount',function(){
    return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
          scope.$watch(attr['ngModel'], function (v) {
            console.log('value changed, new value is: ' + v);
          });
        } 
      } 
    }
};

});

The directive is called within ng-repeat as

<div ng-repeat="ticket in tickets">
    <input my-amount ng-model="ticket.price"></input> 
</div>

Very happy for any help. I don't understand how the scope attribute looks like within an ng-repeat.

Thanks.

解决方案

http://jsbin.com/mihupo/1/edit

attrs instead attr

app.directive('myAmount',function(){
    return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
          scope.$watch(attrs['ngModel'], function (v) {
            console.log('value changed, new value is: ' + v);
          });
        } 
      } ;
    }
);

这篇关于Angularjs:听指令中的模型变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆