Angularjs 注意不要使用ControllerAs语法捕获this.value
手表没有捕捉到对Angularjs 注意不要使用ControllerAs语法捕获this.value,angularjs,Angularjs,手表没有捕捉到对ng model=“billing.inputBoxFilter”的更改。有什么想法吗?没有错误。只是控制台没有输出。日志 <ng-include src="'billing/billing.html'" ng-controller="BillingCtrl as billing"></ng-include> 只需引用此 var vm = this; $scope.$watch
ng model=“billing.inputBoxFilter”
的更改。有什么想法吗?没有错误。只是控制台没有输出。日志
<ng-include
src="'billing/billing.html'"
ng-controller="BillingCtrl as billing"></ng-include>
只需引用此
var vm = this;
$scope.$watch(function() {
return vm.inputBoxFilter
}, function(newValue, oldValue) {
console.log(newValue);
});
问题是回调参数不正确的语法问题(不是错误)。一个关于ControllerAs语法如何破坏可读性并导致代码过多的好例子
$scope.$watch(angular.bind(this, function() {
return vm.inputBoxFilter;
}), function(newValue, oldValue) {
console.log(newValue);
});
定义controllerAs标识符时,控制器实例作为作用域上的属性可用,这就是使用controllerAs语法的原因 是的
如果您遵循始终存储
this
引用的最佳实践并使用该引用,则无需使用bind()。建议在问题中解释整个情况,以便得到最完整的答案。对于Angular 1.5及更高版本,首选的方法是$onChanges hook,它根本不涉及$scope,适合当前的Angular趋势。
var vm = this;
$scope.$watch(function() {
return vm.inputBoxFilter
}, function(newValue, oldValue) {
console.log(newValue);
});
$scope.$watch(angular.bind(this, function() {
return vm.inputBoxFilter;
}), function(newValue, oldValue) {
console.log(newValue);
});
$scope.$watch('billing.inputBoxFilter', function(newValue, oldValue) { ... });