ng-submit未在angularjs中触发 [英] ng-submit not triggering in angularjs

查看:57
本文介绍了ng-submit未在angularjs中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

查看

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

推荐答案

尝试一下:

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

请注意,您在代码中错过了两个'; '.建议您使用 JSHint 之类的工具定期测试JS代码.

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

这篇关于ng-submit未在angularjs中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆