[英]scope.$watch does not get triggered after scope.$digest in karma unit test
[英]Updating a form input name value after a $scope.$watch is triggered
我正在使用AngularJS 1.4。 我有一个表单输入元素,并且我试图动态设置name属性。
<input type="email" name="{{ctrl.name}}" class="form-control" id="email" ng-minlength="5" required>
只要在控制器运行时声明并分配了name
,即可按预期分配name
属性。 但是,在控制器使用$scope.$watch
检测到参数更改后,我需要它工作。
function FormFieldCtrlFtn($scope, $log) {
var vm = this;
vm.name = ''; // works if I provide a name here
$scope.$watch(function(scope) {
return (vm.obj);
}, function(newval, oldval) {
if(newval) {
vm.name = newval.name; // need it to work when the name is updated here
$log.info('name: ' + vm.name);
}
});
}
即使$scope.$watch
被触发,并且vm.name
变量被分配了一个有效值,在我的表单中也没有检测到该值。 有解决办法吗?
如果您尝试观察范围内的特定变量,我建议您进行更改:
$scope.$watch(function(scope) {
...
});
至:
$scope.$watch('varYouAreWatching', function(newValue, oldValue) {
...
});
其中varYouAreWatching
是您要观看的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.