繁体   English   中英

错误:ng:areq错误参数:参数'registerController'不是函数,未定义

[英]Error: ng:areq Bad Argument: Argument 'registerController' is not a function, got undefined

我对angular不熟悉。我尝试在登录页面中插入一个用于node.js api集成的新控制器,但出现上述错误。我看到了相关问题,但无法解决我的问题。赞赏。 提前致谢。

registerCtrl.js

 /*jslint node: true */
   /*jslint white: true */
  (function() {
 'use strict';

 angular.module('registerCtrl', [])  

 .controller('registerController', function($scope, $http, $templateCache) {

  var register = this;
  var method = 'POST';
  var inserturl = 'http://localhost:8080/v1/auth/login';// URL where the Node.js server is running
  $scope.codeStatus = "";
  $scope.save = function() {
    // Preparing the Json Data from the Angular Model to send in the Server. 
    var formData = {
      'email' : this.email,
      'password' : this.password
    };

    this.password = '';
    this.email = '';

    var jdata = 'mydata='+JSON.stringify(formData); // The data is to be string.

    $http({ // Accessing the Angular $http Service to send data via REST Communication to Node Server.
            method: method,
            url: inserturl,
            data:  jdata ,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            cache: $templateCache
        }).
        success(function(response) {
        console.log("success"); // Getting Success Response in Callback
                $scope.codeStatus = response.data;
        console.log($scope.codeStatus);

        }).
        error(function(response) {
        console.log("error"); // Getting Error Response in Callback
                $scope.codeStatus = response || "Request failed";
        console.log($scope.codeStatus);
        });
        return false;
  };    

});
}());

register.html

<div ng-controller="registerController as register">
<div class="container">    
    <div id="loginbox" ng-show="signin-value"  style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">                    
        <div class="panel panel-info" >
                <div class="panel-heading">
                    <div class="panel-title">Sign In</div>
                    <div style="float:right; font-size: 80%; position: relative; top:-10px"><a href="#">Forgot password?</a></div>
                </div>     

                <div style="padding-top:30px" class="panel-body" >

                    <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>

                    <form id="loginform" class="form-horizontal" role="form" ng-submit="save()">

                        <div style="margin-bottom: 25px" class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                                    <input id="login-username" type="text" class="form-control" name="username" ng-model="email" value="" placeholder="email">                                        
                                </div>

                        <div style="margin-bottom: 25px" class="input-group">
                                    <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                                    <input id="login-password" type="password" class="form-control" ng-model="password" name="password" placeholder="password">
                                </div>

                        <div class="input-group">
                                  <div class="checkbox">
                                    <label>
                                      <input id="login-remember" type="checkbox" name="remember" value="1"> Remember me
                                    </label>
                                  </div>
                                </div>


                            <div style="margin-top:10px" class="form-group">
                                <!-- Button -->

                                <div class="col-sm-12 controls">
                                  <a id="btn-login" href="#" class="btn btn-success">Login  </a>

                                </div>
                            </div>


                            <div class="form-group">
                                <div class="col-md-12 control">
                                    <div style="border-top: 1px solid#888; padding-top:15px; font-size:85%" >
                                        Don't have an account! 
                                    <a href="#" ng-click="login-value= false;signup-value=true;">
                                        Sign Up Here
                                    </a>
                                    </div>
                                </div>
                            </div>    
                        </form>     
                    </div>                     
                </div>  
    </div>

我还在index.html中添加了js文件作为

    <script src="app/controllers/registerCtrl.js"></script>

您的registerController函数尚未注入到应用程序控制器中:

registerCtrl.controller('registerController', registerController)

另外,在模板中,您必须使用ng-app来声明应用程序的开始位置,并且控制器需要嵌套在其中,如下所示:

<div ng-app="registerCtrl">
    <div ng-controller="registerController">

还有一件事,为什么将应用程序命名为registerCtrl 不是控制器吗?

最后,末端的圆括号排列顺序不正确。 改成:

)();

());

从注释中可以很明显地看出,您的应用程序中没有包含registerCtrl模块作为依赖项,因此registerController控制器对您的应用程序不可用。

就像在appRoutes使用了registerController控制器appRoutes ,您应该在appRoutes包括模块依赖项。 例如(请注意,这可能并不完全正确)...

angular.module('appRoutes', ['ngRoute', 'registerCtrl'])

暂无
暂无

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

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