简体   繁体   English

ng-submit未在angularjs中触发

[英]ng-submit not triggering in angularjs

I got no error in my console, but my ng-submit is just not triggering. 我的控制台没有错误,但是我的ng-submit只是没有触发。 I debug by putting something in my controller, it did trigger, means it's not the problem of my controller not loading.. Below is my source code 我通过在控制器中放入一些东西进行调试,它确实触发了,这不是控制器未加载的问题。以下是我的源代码

(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??
   }
}
});

View 视图

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

try this: 尝试这个:

signup.js 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 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>

As a side note, you missed two ' ; 附带说明,您错过了两个' ;; ' in your code. 在您的代码中。 I suggest you regularly test your JS code with a tool like JSHint . 我建议您使用JSHint之类的工具定期测试JS代码。

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

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