Angular - ng-model 更改时,ng-change 不会触发 [英] Angular - ng-change not firing when ng-model is changed

查看:115
本文介绍了Angular - ng-model 更改时,ng-change 不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入如下:

action() 在我手动输入和更改输入时执行.但是,如果我通过其他一些函数以编程方式更改 repair.test 值,它不会触发 ng-change 的操作.我已阅读 angular 教程,这可能是预期的行为.

https://docs.angularjs.org/api/ng/directive/ngChange

当值更改来自模型时,不会评估表达式."<- 我也需要这个.当模型以任何方式发生变化时,如何在控制器中触发动作?(输入输入或通过任何其他功能)

感谢您的帮助.

模型值实际上是 form.$valid,它有自己的 Form 控制器(我认为),这就是为什么我使用 action 函数尝试将值传递给父控制器.所以 $scope.$watch 目前不起作用,只有在初始化时才起作用.

解决方案

ngChange 仅用于输入,如果您想听模型,请这样做

$scope.$watch('repair.test', function(newvalue,oldvalue) {});

The input is the following:

<input type="text" ng-model="repair.test"  ng-change="action()" />

The action() is executed when I manually type and change the input. However if I change the repair.test value by some other function programmatically, it doesnt fire the ng-change's action. I have read the angular tutorial and it's probably the expected behavior.

https://docs.angularjs.org/api/ng/directive/ngChange

"The expression is not evaluated when the value change is coming from the model." <- I need this too. How can I fire an action in controller, when the model changes in any way? (typing in input or by any other function)

Thanks for the help.

Edit:

The model value is actually the form.$valid, which has it's own Form controller around it (I think), that is why I used the action function to try to pass the value to the parent controller. So $scope.$watch at the moment doesn't work, only when it is initialised.

解决方案

ngChange is just for the input, if you want to listen the model do like this

$scope.$watch('repair.test', function(newvalue,oldvalue) {

            });

这篇关于Angular - ng-model 更改时,ng-change 不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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