Javascript 模型更改时的角度响应

Javascript 模型更改时的角度响应,javascript,angularjs,Javascript,Angularjs,您将如何对模型更改做出反应以触发进一步的操作?例如,假设您有一个名为email的输入文本字段,您希望在用户开始键入电子邮件时触发或执行一些代码 HTML标记: <input type="email" ng-model="app.email"> 我们可以在控制器中使用$watch功能实现这一点: function MyCtrl($scope) { $scope.email = ""; $scope.$watch("email", function(newValue

您将如何对模型更改做出反应以触发进一步的操作?例如,假设您有一个名为email的输入文本字段,您希望在用户开始键入电子邮件时触发或执行一些代码

HTML标记:

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

我们可以在控制器中使用$watch功能实现这一点:

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');
        }


我们可以在控制器中使用$watch功能实现这一点:

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');
        }



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