Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 注意不要使用ControllerAs语法捕获this.value_Angularjs - Fatal编程技术网

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) { ... });