我是角色新手,我有这个问题,我有服务,想把它注入我的控制器。
我关注了示例,但是我的用户服务出现了注入错误,这是我的代码:
var twApp = angular.module("test",['ngRoute']);
twApp.config(function($routeProvider){
$routeProvider.when('/',{
templateUrl: "templates/home.html",
controller: "HomeController"
})
.when('/setting',{
templateUrl: "templates/setting.html",
controller: "SettingController"
})
.otherwise({redirectTo:'/'});
});
twApp.service("User",["$scope",function($scope){
var IsAuthenticated= function(){
alert("This function is called");
};
return{
IsAuthenticated:IsAuthenticated
};
}]);
twApp.controller("HomeController",["$scope","User",function($scope,User){
User.IsAuthenticated();
}]);
我的问题在哪里?
我收到了这个错误:
Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- User
http://errors.angularjs.org/1.3.14/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20User
minErr/<@http://localhost/angular.js:63:12
createInjector/providerCache.$injector<@http://localhost/angular.js:3997:19
getService@http://localhost/angular.js:4144:39
createInjector/instanceCache.$injector<@http://localhost/angular.js:4002:28
getService@http://localhost/angular.js:4144:39
invoke@http://localhost/angular.js:4176:1
enforcedReturnValue@http://localhost/angular.js:4038:20
invoke@http://localhost/angular.js:4185:14
createInjector/instanceCache.$injector<@http://localhost/angular.js:4003:20
getService@http://localhost/angular.js:4144:39
invoke@http://localhost/angular.js:4176:1
instantiate@http://localhost/angular.js:4193:27
$ControllerProvider/this.$get</<@http://localhost/angular.js:8462:18
ngViewFillContentFactory/<.link@http://localhost/angular-route.js:975:26
invokeLinkFn@http://localhost/angular.js:8219:9
nodeLinkFn@http://localhost/angular.js:7729:1
compositeLinkFn@http://localhost/angular.js:7078:13
publicLinkFn@http://localhost/angular.js:6957:30
createBoundTranscludeFn/boundTranscludeFn@http://localhost/angular.js:7096:1
controllersBoundTransclude@http://localhost/angular.js:7756:18
update@http://localhost/angular-route.js:933:25
$RootScopeProvider/this.$get</Scope.prototype.$broadcast@http://localhost/angular.js:14720:15
commitRoute/<@http://localhost/angular-route.js:616:15
processQueue@http://localhost/angular.js:13189:27
scheduleProcessQueue/<@http://localhost/angular.js:13205:27
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://localhost/angular.js:14401:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost/angular.js:14217:15
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost/angular.js:14506:13
done@http://localhost/angular.js:9659:36
completeRequest@http://localhost/angular.js:9849:7
requestLoaded@http://localhost/angular.js:9790:1
<div class="ng-scope" ng-view="">
这是我的身体代码:
<body>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="angular-route.js"></script>
<script type="text/javascript" src="app.js"></script>
<!--<span ng-controller="GeneralController"></span>-->
This it simple home page
<a href="/#/">Home</a>
<a href="/#/setting">Setting</a>
<div ng-view></div>
</body>
答案 0 :(得分:2)
错误说,服务中的$ scope注入有问题。因此服务没有编译。
尝试将$ scope注入到服务中,看看是否一切正常。
注意:理想情况下,$ scope应该只与您的控制器和指令一起使用。
答案 1 :(得分:0)
尝试将twApp.service(...)
替换为twApp.factory(...)
。
由于您要返回一个对象,您创建的内容实际上是一个工厂,因此这可能是您的问题。
否则,请包含错误消息。
答案 2 :(得分:0)
对我来说,当我使用file://pathtohtmlpage.html时,我遇到了这些问题,当我在Netbeans上的apache服务器上托管我的页面时,问题就解决了。你在做吗?
您是否也确定您的代码是在ng-app ='test'下?
问候