当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - ng-submit not triggering in angularjs

问题描述:

I got no error in my console, but my ng-submit is just not triggering. 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

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

As a side note, you missed two ';' in your code. I suggest you regularly test your JS code with a tool like JSHint.

分享给朋友:
您可能感兴趣的文章:
随机阅读: