我是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>
答案 0 :(得分:1)
您的registerController
函数尚未注入应用程序控制器:
registerCtrl.controller('registerController', registerController)
此外,在您的模板中,您必须使用ng-app
来声明应用程序的启动位置,并且您的控制器需要嵌套在其中,如下所示:
<div ng-app="registerCtrl">
<div ng-controller="registerController">
还有一件事,为什么要为您的应用程序registerCtrl
命名?它不是控制器吗?
最后,您在末尾的圆括号排序不正确。改为:
)();
来自
());
答案 1 :(得分:1)
从评论中可以清楚地看出,您的应用中没有任何内容将registerCtrl
模块作为依赖项包含在内,因此您的应用无法使用registerController
控制器。
在registerController
中使用appRoutes
控制器,您应该在那里包含模块依赖项。例如(注意这可能不完全正确)......
angular.module('appRoutes', ['ngRoute', 'registerCtrl'])