Reputation: 39
I'm new in angular and I have this issue, I have service and want to inject it to my controller.
I followed samples but I get injection error on my user service, this is my code:
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();
}]);
where is my problem?
And I get this error:
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="">
And here is my body code:
<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>
Upvotes: 0
Views: 509
Reputation: 614
For me i've got these problem when i was using file://pathtohtmlpage.html, and the problem was fix when I hosted my pages on an apache server on Netbeans. Are you doing that ?
Are you also sure that you code is under ng-app='test' ?
regards
Upvotes: 0
Reputation: 362
The error says, $scope injection in the service has a problem. hence the service is not compiled.
try removing $scope injection into service and see if everything works.
note: $scope ideally should be with your controller and directives only.
Upvotes: 2
Reputation: 19193
Try to replace twApp.service(...)
with twApp.factory(...)
.
Since you are returning an object, what you are creating is in fact a factory, so that may be your problem.
Otherwise, please include the error message.
Upvotes: 0