javascript 以编程方式更改模型时 ngChange 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27536772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ngChange doesn't work when the model is changed programmatically?
提问by Adam Zerner
It says in the docsthat ngChange
will not fire: "if the model is changed programmatically and not by a change to the input value".
它在不会触发的文档中说ngChange
:“如果模型以编程方式更改而不是通过更改输入值”。
Does this mean that if you everchange the model programmatically, you can't use ngChange
?
这是否意味着,如果你曾经改变模型编程,你不能用ngChange
?
Or does it mean that you can't use ngChange
if:
或者这是否意味着您不能在以下情况下使用ngChange
:
1) You change the model programmatically
1)您以编程方式更改模型
AND
和
2) You're unable to change the model via the input field
2) 您无法通过输入字段更改模型
回答by Wayne Ellery
It just means that the ngChange expression won't be evaluated if javascript is used to change the model. If you wanted ngChange to fire you would need to programmatically call the expression similar to the following:
这只是意味着如果使用 javascript 更改模型,则不会评估 ngChange 表达式。如果您希望 ngChange 触发,您需要以编程方式调用类似于以下内容的表达式:
<input type="checkbox" ng-model="confirmed" ng-change="change()" id="ng-change-example1" />
You would need to manually call the change function if you wanted it to fire:
如果您希望它触发,则需要手动调用更改函数:
$scope.confirmed = 'test';
$scope.change();
回答by arman1991
If I read and understand this correctly, unfortunatelly you can't do it with ng-change as you say, but I'm not 100% sure.
如果我正确阅读并理解了这一点,不幸的是你不能像你说的那样用 ng-change 做到这一点,但我不是 100% 确定。
I was thinking about ng-model-options, a new feature that came with AngularJS 1.3 and maybe this can push you forward.
我在考虑ng-model-options,这是 AngularJS 1.3 附带的一个新功能,也许这可以推动你前进。
There is an automatical options for getters and setters that allows you to change the model in real time. Theoretically, you could use this and call your update function with ng-bind.
getter 和 setter 有一个自动选项,允许您实时更改模型。从理论上讲,您可以使用它并使用ng-bind调用您的更新函数。
Maybe this can be your possible solution to solve your problem...
也许这可以成为您解决问题的可能解决方案......
(function(angular) {
'use strict';
angular.module('getterSetterExample', [])
.controller('ExampleController', ['$scope', function($scope) {
var _name = 'Brian';
$scope.user = {
name: function(newName) {
return angular.isDefined(newName) ? (_name = newName) : _name;
}
};
}]);
})(window.angular);
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-ngModelOptions-directive-getter-setter-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.7/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="getterSetterExample">
<div ng-controller="ExampleController">
<form name="userForm">
Name:
<input type="text" name="userName"
ng-model="user.name"
ng-model-options="{ getterSetter: true }" />
</form>
<pre>user.name = <span ng-bind="user.name()"></span></pre>
</div>
</body>
</html>