例如,我有这个:
<input type="text" ng-model="model_1" value="{{model_2}}" />
Show me {{model_1}}
问题是,当 model_2 更改时,“Show me {{model_1}}”不会更改为新值。
最佳答案
<input type="text" ng-model="model_2" />
Show me <span ng-bind="model_1" />
在你的 Controller 中
scope.$watch('model_2', function() {
scope.model_1 = scope.model_2
});
更新:由于您确实希望一个作用域属性是其他两个属性的总和,因此您需要同时观察它们
scope.$watch('model_1', updateModel3);
scope.$watch('model_2', updateModel3);
function updateModel3(){
scope.model_3 = scope.model_1 + scope.model_2
}
带有标记
<input ng-model='model_1' />
<input ng-model='model_2' />
<span ng-bind='model_3' />
关于javascript - AngularJS:如果ng-model被另一个模型填充,如何使其影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19013543/