我有一个绑定(bind)到模型值的文本输入,但我不知道如何以编程方式更改输入值并将更改传播到模型。
我明白,因为我正在更新 Angular 范围之外的值,所以我需要显式调用 $scope.$apply()
,但它不起作用。
HTML:
<input id="test_input" ng-model="test_value">
Controller :
$scope.test_value = 'abc'; // starting value
控制台:
$('#test_input').val('xyz');
$('#test_input').scope().$apply();
$('#test_input').scope().test_value;
-> 'abc';
最佳答案
ngModel 监听“input”事件,所以你需要在设置值后触发该事件:
$('#test_input').val('xyz');
$('#test_input').trigger('input');
$('#test_input').scope().test_value;
关于javascript - 以编程方式更改输入时, Angular 模型不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37777889/