简体   繁体   English

ng-click控制器中不触发功能

[英]ng-click not fire function in controller

The bug is very weird, I have been stucking here for hours just can find why my function in controller is fired. 该错误非常奇怪,我在这里呆了几个小时才发现为什么我的控制器功能被解雇了。

Here is my HTML code: 这是我的HTML代码:

<div class="col-md-9 clearfix" id="customer-account" ng-controller='ProfileController'>
  <div class="box clearfix">
    <div class="row">
      <div class="col-sm-6">
        <div class="form-group">
          <label for="password_old">oldpassword</label>
          <input type="password" class="form-control" id="password_old" ng-model="passwordold">
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-6">
        <div class="form-group">
          <label for="password_1">newpassword</label>
          <input type="password" class="form-control" id="password_1" ng-model="passwordnew1">
        </div>
      </div>
      <div class="col-sm-6">
        <div class="form-group">
          <label for="password_2">retype</label>
          <input type="password" class="form-control" id="password_2"ng-model="passwordnew2">
        </div>
      </div>
      <p>{{passwordnew2}}</p>
    </div>
    <!-- /.row -->

    <div class="text-center">
      <button type="submit" class="btn btn-primary">
        <i class="fa fa-save" ng-click="changePwd(passwordold,passwordnew1,passwordnew2)"></i> save
      </button>
    </div>
    <div class="text-center" >
      <p>{{errorMessageChangepwd}}aaaa</p>
    </div>
  </div>
</div>

After I click the Button, which ng-click attribute as you can see, nothing happen. 单击按钮后,您可以看到ng-click属性,什么也没有发生。

Here is my controller code: 这是我的控制器代码:

controller('ProfileController', ['$scope','UserFactory','SharedDataFactory',
        function($scope,UserFactory,SharedDataFactory){
                $scope.user = UserFactory.user;
                $scope.passwordold;
                $scope.errorMessageChangepwd='error';
                $scope.showErrMsgChangepwd = false;
                $scope.passwordnew1;
                $scope.passwordnew2;
                $scope.changePwd = function(passwordold,passwordnew1,passwordnew2){
                       console.log("aaaaaaaaaa");
                       if (passwordnew1!==passwordnew2){
                          $scope.showErrMsgChangepwd= true;
                          $scope.errorMessageChangepwd = 'error';
                        }else{
                          UserFactory.changePwd(passwordnew1)
                          .catch(function(err){
                                 console.log(err.data);
                          }).then(function(response){
                              console.log(response);
                          });
                        }
                };}]);

I called console.log("aaaaaaaaaa"); 我叫console.log("aaaaaaaaaa"); in the first line of my function, but after I click the button, nothing is shown on console. 在函数的第一行中,但是单击按钮后,控制台上没有任何显示。 And also 并且

<div class="text-center" >
 <p>{{errorMessageChangepwd}}aaaa</p>
</div>`

does not show error aaaa as expected but aaa on the browser. 不会按预期显示错误aaaa ,但在浏览器上显示aaa。 what could be wrong? 有什么问题吗?

Thanks. 谢谢。

You need to add ng-controller="ProfileController" in the top of your form. 您需要在表单顶部添加ng-controller =“ ProfileController”。 Which looks like missing on your code. 看起来好像缺少您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM