AngularJS ng-change不调用函数

时间:2013-11-08 22:53:31

标签: javascript angularjs angular-ui

当用户在电子邮件文本框中键入内容时,我正在尝试使用引导程序弹出框中的内容进行更改。似乎ng-change没有进入方法updateToolTip()。我是AngularJS的新手。任何建议都表示赞赏。

html页面

<div ng-controller="LoginController">

  <for name="form" ng-submit="login()">

    <input type="email" name="email" ng-model="user.email" value="{{user.email}}" ng-change="updateToolTip()" popover="{{emailMessage}}" popover-trigger="focus" popover-placement="right" required>

    <button class="btn btn-lg btn-primary btn-block" type="submit" ng-disabled="form.$invalid">Sign in</button>
  </form>

</div>

JS

var loginModule = angular.module('loginModule', ['ui.bootstrap']);

// Controller for the login page
loginModule.controller('LoginController', ['$scope', '$http', function($scope, $http) {

    $scope.emailMessage = 'test';   

    $scope.updateToolTip = function() {

        $scope.emailMessage = 'asdfsadf';   
        console.log();
        console.log(' inside function');
        if($scope.user != null) {
            console.log('user not null');
            if($scope.user.email.$dirty && $scope.user.email.$error.email) {
                console.log('email dirty and error');
                $scope.emailMessage = 'Invalid Email!';
            } else if($scope.form.email.$dirty && $scope.form.email.$error.required) {
                console.log('emaildirty and required');
                $scope.emailMessage = 'Email Required';
            }
        }
    }
}]);

1 个答案:

答案 0 :(得分:4)

form名称更改为name="user"。像

 <form class="form-signin" name="user" ng-submit="login()">

由于您使用的表单模型如user.XXXX

演示 Plunker

<强> [编辑]

我会写这样的验证:

<input 
   type="email"
   name="email"
   class="form-control"
   placeholder="Email address"
   ng-model="user.email"
   popover="{{emailMessage}}"
   popover-trigger="focus"
   popover-placement="right"
   required
   >
    <span class="error" ng-show="mailform.email.$error.required">required</span>
    <span class="error" ng-show="mailform.email.$error.email">invalid email</span>


    <input 
    type="password"
    name="password"
    class="form-control"
    placeholder="Password"
    ng-model="user.password"
    value="{{user.password}}" required    >
    <span class="error" ng-show="mailform.password.$error.required">required</span>

演示2 Plunker

也许这个例子也可以帮到你:演示3 Plunker