ng-submit不在angularjs中触发

时间:2016-09-04 04:29:58

标签: javascript angularjs

我的控制台没有错误,但我的ng-submit没有触发。我通过在控制器中放置一些东西进行调试,它确实触发了,意味着它不是我的控制器没有加载的问题。下面是我的源代码

(function() {
angular.module('MyApp')
    .controller('SignupCtrl', SignupCtrl);

SignupCtrl.$inject = ['$scope', '$rootScope', '$location', '$window', '$auth'];

function SignupCtrl($scope, $rootScope, $location, $window, $auth) {
  console.log('hello') // triggered
    var ctrl = this;
    ctrl.signup = signup;

    function signup() {
      console.log('trigger') // nope??
   }
}
});

查看

<form ng-submit="signup()">
<div class="form-group">
  <label for="name">First Name</label>
  <input required type="text" name="fname" id="fname" placeholder="First Name" class="form-control" ng-model="user.fname" autofocus>
</div>
<button type="submit" class="btn btn-success">Sign up</button>
</form>

1 个答案:

答案 0 :(得分:1)

试试这个:

signup.js

(function () {
    /* jshint validthis: true */
    'use strict';

    angular
        .module('MyApp')
        .controller('Signup', Signup);

    Signup.$inject = ['$scope', '$rootScope', '$location', '$window', '$auth'];

    function Signup($scope, $rootScope, $location, $window, $auth) {
        var vm = this;
        vm.signup = signup;
        vm.user = ...;

        function signup() {
            console.log('trigger');
        }
    }
})();

signup.html

<section id="signup-view" data-ng-controller="Signup as vm">
    <form>
        <div class="form-group">
            <label for="name">First Name</label>
            <input required type="text" name="fname" id="fname" placeholder="First Name" class="form-control" ng-model="vm.user.fname" autofocus>
        </div>
        <button class="btn btn-success" data-ng-click="vm.signup()">Sign up</button>
    </form>
</section>

作为旁注,你错过了两个&#39; ; &#39;在你的代码中。我建议您使用JSHint等工具定期测试您的JS代码。