AngularJS表单提交

时间:2015-11-17 15:36:11

标签: javascript angularjs forms ng-submit

我有一张表单来收集用户的注册详细信息,其定义如下:

<form class="m-t-xl" ng-submit="signup(user)" novalidate>
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Name" required="" ng-model="user.name">
            </div>
            <div class="form-group">
                <input type="email" class="form-control" placeholder="Email" required="" ng-model="user.email">
            </div>
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Company" required="" ng-model="user.company">
            </div>
            <div class="form-group">
                <input type="password" class="form-control" placeholder="Password" required="" ng-model="user.password">
            </div>
            <div class="form-group">
                <div class="checkbox i-checks"><label> <input type="checkbox"><i></i> Agree the terms and policy </label></div>
            </div>
            <button type="submit" class="btn btn-primary block full-width m-b">Register</button>

            <p class="text-muted text-center"><small>Already have an account?</small></p>
            <a ui-sref="login" class="btn btn-sm btn-white btn-block">Login</a>
</form>

出于某种原因,当我点击提交按钮时,没有任何反应。按钮刚刚死了。 我的控制器有以下代码:

.controller('registerCtrl', ['$scope', '$state', 'Registration', 
                            function($state, $scope, Registration) {

    $scope.user = {
        name: '',
        email: '',
        company: '',
        password: ''
    };
    console.log('controller getting loaded');
    $scope.signup = function(user) {
        console.log('register getting called');
    }
}]);

表现为表格不会识别&#34;注册&#34;方法。以类似的方式,我实现了一个登录表单:

<form class="m-t" ng-submit="login(credentials)" novalidate>
            <div class="form-group">
                <input type="email" class="form-control" placeholder="Email" required="" ng-model="credentials.email">
            </div>
            <div class="form-group">
                <input type="password" class="form-control" placeholder="Password" required="" ng-model="credentials.password">
            </div>
            <button type="submit" class="btn btn-primary block full-width m-b">Login</button>

            <a ui-sref="forgot_password"><small>Forgot password?</small></a>
            <p class="text-muted text-center"><small>Do not have an account?</small></p>
            <a class="btn btn-sm btn-white btn-block" ui-sref="register">Create an account</a>
</form>

.controller('loginCtrl', ['$scope', '$rootScope', '$state', 'Authentication', 'AUTH_EVENTS', 
                        function($scope, $rootScope, $state, Authentication, AUTH_EVENTS) {

    $scope.credentials = {
        email: '',
        password: ''
    };

    $scope.login = function(credentials) {
        Authentication.login(credentials).then(function(user) {
            $rootScope.$broadcast(AUTH_EVENTS.loginSuccess);
            $scope.setCurrentUser(user);
            $state.go('events');
        }, function () {
            $rootScope.$broadcast(AUTH_EVENTS.loginFailed);
        });
    }
}]);

第二种形式正在按预期工作......我不知道发生了什么。我想说我记录了 registerCtrl 并且它已正确加载到页面中,但是这个方法在调用时没有做任何事情。

对于其他可能导致此问题的任何想法?我检查了所有解决方案,但似乎没有任何帮助。

在我的config.js中,两个路由都被类似地声明:

.state('login', {
            url: "/login",
            templateUrl: "views/login.html",
            data: { pageTitle: 'Login', specialClass: 'gray-bg' }
        })
        .state('register', {
            url: "/register",
            templateUrl: "views/register.html",
            data: { pageTitle: 'Register', specialClass: 'gray-bg' }
        })

我还尝试更改我的表单以点击&#34;注册&#34;调用相同方法的按钮,但这具有相同的行为。

谢谢!

1 个答案:

答案 0 :(得分:2)

在“registerCtrl”控制器中,你有$ scope和$ state mixed:

.controller('registerCtrl', ['$scope', '$state', 'Registration', 
                        function($state, $scope, Registration) {}]);

应该是:

.controller('registerCtrl', ['$scope', '$state', 'Registration', 
                        function($scope, $state, Registration) {}]);

我认为问题在于您将用户对象和登录功能分配给错误的应用程序对象。