按钮使用angularjs单击密码验证

时间:2016-04-21 09:41:31

标签: jquery angularjs

在我的要求中,我想验证密码并通过按钮确认。如果我输错了,我的方法就不会打电话。如果我输入正确那么它会起作用。我试过请帮我解决这个问题,这是我的fiddle

HTML

app.directive(.., ['gchart', function(.., gchart) {
   return {
      //...
      link: function(scope, elm, attr) {
          scope.init = function() {
              var viz = new google.visualization.arrayToDataTable(data);
              //add options, linechart, draw, whatever
          };

          // delay the creation until jsapi files loaded
          loadGoogleChartAPI.then(function () {
              scope.init();
          }, function () {
              //ignore
          });           
       }
   }
}

JS

<body ng-app="app" ng-controller="Ctrl" >
    <form data-toggle="validator" role="form" name="myForm">Name<inputtype="text" id="name" ng-model="name" required/>Enter password<input type="password" id="txtInput" ng-model="password" required/>    <br />
    Confirm password:  <input type="password" id="txtInput" ng-model="confirmpassword" required/>

    <button  class="btn btn-default" style="background-color: #005baa; color: #fff;"  ng-click=" myForm.$valid && submit()">Submit</button>
    </form>
</body>

1 个答案:

答案 0 :(得分:1)

首先,不要调用$ scope函数submit(),因为它会发送一个常规的submit()表单。

然后,使用角度来获取输入。在控制器中,您可以使用$ scope.myNgModel来获取数据。

以下是处理确认的干净代码:

var app = angular.module('app', []);
app.controller('Ctrl', function($scope, $http) {
    $scope.submitForm = function() {
        if ($scope.password != $scope.confirmpassword) {
            alert("Passwords Don't Match");
        } else {
            doSomeStuff();
        }
    };

    function doSomeStuff() {
        alert("Password = " + $scope.password);
    }
});

https://jsfiddle.net/6jqmv68d/10/