在角度js

时间:2015-06-28 16:47:59

标签: angularjs forms validation angularjs-directive angularjs-forms

我有一个简单的自定义指令,带有对URL的http post调用。当单击提交按钮时,它应该调用url,因为自定义指令属性放在标记内。

我在Chrome控制台中检查过它没有被调用。我不确定我哪里出错了。

代码:

<!DOCTYPE html>
<html ng-app="myApp" >
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        <link rel="stylesheet" href="style.css" />
        <script>
            var app = angular.module('myApp',[]);
            app.directive('sendMail', function ($http) {
                return {
                    restrict: 'A',
                    require: 'ngModel',
                    link: function (scope, element, attrs, ngModel) {
                        $http.post('MailSenderServlet.do').success(function (data) {
                        });
                    }
                };
            });
        </script>
        <title>Registration Form</title>
    </head>
    <body ng-controller="MainCtrl">
        <div class="container">
            <h2 class="text-muted">Registration form</h2><br/>
            <div>
                <form name="myForm" action="RegistrationServlet.do" method="POST" novalidate>
                    <div class="form-group has-feedback">
                        <label class="control-label">First name:</label> <input type="text" class="form-control input-sm " name="uname" ng-pattern="/^[a-zA-Z]{3,20}/" ng-model="user.uname" placeholder="First Name" required/>
                        <span style="color:red" ng-show="myForm.uname.$error.pattern">First name cannot be less than 3 letters with no digits</span>
                        <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.uname.$error.required">Please fill field above<br></span>
                        <span style="color:red" class="hide-while-in-focus" ng-show="myForm.uname.$error.unique">Username already exist<br/></span>
                        <span ng-if="myForm.uname.$valid" class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
                    </div>
                    <button class="form-control btn btn-success" type="submit" name="submit" ng-model="submit" send-mail ">Submit</button>
                </form>
            </div>            
        </body>
    </html>

3 个答案:

答案 0 :(得分:1)

它应该在指令加载后立即调用,在链接函数中放置alert();并删除链接函数中的当前项,您将看到alert();

DEMO

但是如果要在单击按钮后立即调用函数,则需要ng-click指令和控制器函数在单击指令控制器或链接函数后执行。

<button  ng-click="sendData()" class="form-control btn btn-success" type="submit" name="submit" ng-model="submit" send-mail>Submit</button>

ng-click指令已添加。

app.directive('sendMail', function($http) {
    return {
      restrict: 'A',
      require: 'ngModel',
      link: function(scope, element, attrs, ngModel) {
        //alert();
      },
      controller: function($scope) {
        $scope.sendData = function() {
          alert('send data here');
        }
      }
    };
});

这是一个DEMO

答案 1 :(得分:1)

我更喜欢你不应该去指令,如果它只包含一个ajax调用。您只需使用ng-click / ng-submit(实际上是表单形式)就可以完成。这里不需要指令。

您需要将以下某些内容更正为您的代码。

  1. 我们不需要使用ng-model提交按钮,因为它不包含任何值,所以没有意义。
  2. 此外,您无需在表单上添加操作和方法属性,因为无论如何您都是使用JavaScript代码制作的。
  3. 虽然提交表单angular已经提供了ng-submit的指令。使用它会更有意义。
  4. 您需要在帖子$http.post电话中传递数据。
  5. <强>标记

    <form name="myForm" ng-submit="submit(myForm)" novalidate>
        <div class="form-group has-feedback">
            <label class="control-label">First name:</label>
            <input type="text" class="form-control input-sm " name="uname" ng-pattern="/^[a-zA-Z]{3,20}/" ng-model="user.uname" placeholder="First Name" required/>
            <span style="color:red" ng-show="myForm.uname.$error.pattern">First name cannot be less than 3 letters with no digits</span>
            <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.uname.$error.required">Please fill field above<br></span>
            <span style="color:red" class="hide-while-in-focus" ng-show="myForm.uname.$error.unique">Username already exist<br/></span>
            <span ng-if="myForm.uname.$valid" class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
        </div>
        <button class="form-control btn btn-success" type="submit" name="submit" send-mail="">Submit</button>
    </form>
    

    <强>控制器

    $scope.submit = function(form){
       if(form.$valid){
           $http.post('MailSenderServlet.do', {user: $scope.user})
           .success(function (data) {
               //
           });
       }
       else
         alert("Please validate your form")
    }
    

答案 2 :(得分:0)

您的代码问题只是您编写的控制器部分

<body ng-controller="MainCtrl">

添加一个控制器部件,或者只是删除它,代码工作正常。

参见演示http://jsbin.com/hulujarugu/edit?html,console,output

<强> JS

 var app = angular.module('myApp', []);
 app.directive('sendMail', function($http) {
     return {
         restrict: 'A',
         require: 'ngModel',
         link: function(scope, element, attrs, ngModel) {
             alert(1);

             $http.post('MailSenderServlet.do').success(function(data) {});
         }
     };
 });

<强> HTML

    <div class="container">
        <h2 class="text-muted">Registration form</h2><br/>
        <div>
            <form name="myForm" action="RegistrationServlet.do" method="POST" novalidate>
                <div class="form-group has-feedback">
                    <label class="control-label">First name:</label> <input type="text" class="form-control input-sm " name="uname" ng-pattern="/^[a-zA-Z]{3,20}/" ng-model="user.uname" placeholder="First Name" required/>
                    <span style="color:red" ng-show="myForm.uname.$error.pattern">First name cannot be less than 3 letters with no digits</span>
                    <span style="color:red" class="error" ng-if="myForm.$submitted && myForm.uname.$error.required">Please fill field above<br></span>
                    <span style="color:red" class="hide-while-in-focus" ng-show="myForm.uname.$error.unique">Username already exist<br/></span>
                    <span ng-if="myForm.uname.$valid" class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>
                </div>
                <button class="form-control btn btn-success" type="submit" name="submit" ng-model="submit" send-mail >Submit</button>
            </form>
        </div>