2017-01-16 7 views
1

時計がng-model="billing.inputBoxFilter"への変更をキャッチしていません。何か案は?エラーはありません。 console.log時計がControllerAsでthis.valueをキャッチしていません構文

  <ng-include 
      src="'billing/billing.html'" 
      ng-controller="BillingCtrl as billing"></ng-include> 

angular.module('patientBilling') 
    .controller('BillingCtrl', [ 

     this.inputBoxFilter; 
     $scope.$watch(angular.bind(this, function() { 
     return this.inputBoxFilter; 
     }, function(newValue, oldValue) { 
      console.log(newValue); 
     })); 

答えて

1


 <md-input-container flex="50"> 
     <label>Name</label> 
     <input ng-model="billing.inputBoxFilter" type="text" 
      ng-disabled="selectedItem"> 
     <md-icon md-svg-icon="magnify"></md-icon></md-icon> 
     </md-input-container> 
からわずか何も出力はちょうど thisの参照を使用していません。

var vm = this; 

    $scope.$watch(function() { 
    return vm.inputBoxFilter 
    }, function(newValue, oldValue) { 
    console.log(newValue); 
    }); 

DEMO

0

問題は、コールバックの引数が正しくありませんでしたシンタックスの問題(でないエラー)でした。 ControllerAsの構文がどのように読みやすさを破り、余分なコードを生成するかについての良い例です。

$scope.$watch(angular.bind(this, function() { 
    return vm.inputBoxFilter; 
    }), function(newValue, oldValue) { 
     console.log(newValue); 
    }); 
+1

は 'バインドを()'あなたは常に 'this'参照を格納するベストプラクティスに従うと – charlietfl

1

コントローラインスタンスは、controllerAs識別子が定義されている場合はスコープ上のプロパティとして使用できますが、これがcontrollerAs構文が使用される理由です。

それは

$scope.$watch('billing.inputBoxFilter', function(newValue, oldValue) { ... }); 
+0

を通じて、その参照を使用している場合、私はもともとこれを持っていましたが、使用する必要はありません、コールバックでI他のもののために 'this'環境が必要です。 – dman

+1

この場合、 'vm'も適切ではないかもしれません。最も完全な回答を得るためには、質問全体の状況を説明することをお勧めします。 Angular 1.5以上の場合、これを行うための好ましい方法は$ onChangesフックです。$ onChangesフックは$ scopeをまったく含まず、現在の角度の傾向に適合します。 – estus

関連する問題