我得到了一个"未能实例化模块"尝试注入和使用我写的Angular服务时出错。我有一个调用API的HTML页面。我需要能够从API获取返回值并将其存储以供在另一个控制器中使用。
以下是HTML页面:
<!doctype html>
<html ng-app="auth">
<head>
<title>Login AngularJS</title>
</head>
<body>
<div ng-app="auth" ng-controller="Login">
<form method="post" ng-submit="login()" novalidate ng-controller="Login">
DBID<input type="text" name="dbId" value="23" ng-model="dbId" /> UserName
<input type="text" name="username" value="test@test.com" ng-model="username" /> Password
<input type="text" name="password" value="test1234" ng-model="password" />
<input type="submit" id="submit" value="login" />
<p>The Token is: {{token}}</p>
</form>
<script src="Scripts/angular.js"></script>
<script src="Scripts/app/sharedProperties.js"></script>
<script src="Scripts/app/login.js"></script>
</div>
</body>
</html>
&#13;
这是控制器:
var auth = angular.module('auth', ['SharedProperties']);
auth.controller('Login', function ($scope, $http, SharedProperties)
{
$scope.list = [];
$scope.login = function ()
{
if ($scope.dbId) {
$scope.list.push(this.dbId);
$scope.text = '';
var Credentials = new Object();
Credentials.DBID = this.dbId;
Credentials.username = this.username;
Credentials.password = this.password;
$http({
dataType: 'json',
method: 'POST',
url: 'http://localhost:55049/api/Security/Login',
data: Credentials,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic VGVzdEFwcGxpY2F0aW9uVG9rZW46'
}
}).then(function (response)
{
$scope.token = response.data;
});
} else
{
alert("Please add DBID");
}
};
});
&#13;
这是我的服务代码:
var auth = angular.module('auth', []);
auth.service('SharedProperties', function ()
{
var property = '';
return {
getProperty: function ()
{
return property;
},
setProperty: function (value)
{
property = value;
}
};
});
&#13;
答案 0 :(得分:0)
如果要为共享属性使用单独的模块,则应初始化该模块。取代
var auth = angular.module('auth', []);
与
var auth = angular.module('SharedProperties', []);
在您的服务代码中。