Reputation: 1818
I have this code in a script.js file:
scotchApp.factory('myService', function() {
var savedData = {}
function set(data) {
savedData = data;
}
function get() {
return savedData;
}
return {
set: set,
get: get
}
});
scotchApp.controller('SharedController',['$scope', 'myService', function($scope) {
myService.set('hello');
$scope.message = myService.get();
}]);
when I try to upload an html that uses this code I get in chrome (F12 mode): angular.js:12722 ReferenceError: myService is not defined
why?
Upvotes: 1
Views: 1167
Reputation: 193261
You never injected your service into controller. Correct code:
scotchApp.controller('SharedController', ['$scope', 'myService', function($scope, myService) {
myService.set('hello');
$scope.message = myService.get();
}]);
Note controller function signature function($scope, myService) {...}
.
Upvotes: 4
Reputation: 22875
Most propbably you have not added service script in index.html
<script src="src/your_path/script.js" ></script>
Upvotes: 1