2016-09-12 10 views
0

モデルの変更にどのように反応して、何らかのアクションをトリガーしますか?たとえば、電子メールと呼ばれる入力テキストフィールドがあり、ユーザーが電子メールの入力を開始するとすぐにコードを起動または実行したいとします。モデルの変更による角度の反応

HTMLマークアップ:

<input type="email" ng-model="app.email"> 

CTRL:

.controller('controller', function ($scope) { 

}); 

答えて

4
<input ng-change="myFunction()" type="email" ng-model="app.email"> 
+0

おかげで完璧に動作します –

0

我々は、我々のコントローラでは、この使用して$の時計機能を実現することができます。

function MyCtrl($scope) { 
    $scope.email = ""; 

    $scope.$watch("email", function(newValue, oldValue) { 
     if ($scope.email.length > 0) { 
      console.log("User has started writing into email"); 
     } 
    }); 
} 

または我々は、単純なディレクティブを書くことができますが入力イベントを待ち受けます。

.controller('controller', function ($scope) { 
    $scope.changes = 0; 
    $scope.change = function() { 
     console.log('change'); 
     } 

.directive('changeWatch', function() { 
    return { 
     scope: { 
      onchange: '&changeWatch' 
     }, 
     link: function(scope, element, attrs) { 
      element.on('input', function() { 
       scope.$apply(function() { 
        scope.onchange(); 
       }); 
      }); 
     } 
    }; 
}); 
関連する問題