javascript - AngularJS ng-change not calling function - Stack Overflow

I'm trying to get the content in a bootstrap popover to change when the user types something in th

I'm trying to get the content in a bootstrap popover to change when the user types something in the email text box. It seems that ng-change is not getting into the method updateToolTip(). I'm brand new to AngularJS. Any advice is appreciated.

html page

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

I'm trying to get the content in a bootstrap popover to change when the user types something in the email text box. It seems that ng-change is not getting into the method updateToolTip(). I'm brand new to AngularJS. Any advice is appreciated.

html page

<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';
            }
        }
    }
}]);
Share Improve this question asked Nov 8, 2013 at 22:53 CatfishCatfish 19.3k60 gold badges213 silver badges358 bronze badges 8
  • Can you create a plunker / jsfiddle so we can see it in action? – jpmorin Commented Nov 8, 2013 at 22:56
  • @Catfish without going into the depths of your code, are you using Jquery as well ? I had problems with the two woking together and finally moved the JS loading into the page footer – avrono Commented Nov 8, 2013 at 22:57
  • Not related to the question, but when using ng-model on an input, there is no need to set the value attribute as it will be handled by ng-model! – jpmorin Commented Nov 8, 2013 at 22:57
  • can you post the plete HTML ? – avrono Commented Nov 8, 2013 at 22:59
  • plnkr.co/edit/2pPR60ybLwv1VvIudy8m?p=preview – Catfish Commented Nov 8, 2013 at 23:01
 |  Show 3 more ments

1 Answer 1

Reset to default 4

Change form name to name="user". Like

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

Since you use form model like user.XXXX.

Demo Plunker

[EDIT]

I would write validation like this:

<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>

Demo 2 Plunker

And maybe this example might help you as well: Demo 3 Plunker

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745445950a4628048.html

相关推荐

  • javascript - AngularJS ng-change not calling function - Stack Overflow

    I'm trying to get the content in a bootstrap popover to change when the user types something in th

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信