I am trying to display data in an angular app using $resource, from mongolab. I added dependency to ngResource in the module. Still it says Unknown provider. What is the missing point here?
'$resource is not defined'
Note: When my factory name was not correct, I was getting error: Unknown provider: employeesProvider <- employees
Code
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-resource.js"></script>
<script type="text/javascript">
//defining module
var app = angular.module('myApp', ['ngResource']);
//defining factory
app.factory('employees', function () {
return $resource('https://api.mlab.com/api/1/databases/humanresource/collections/Employees',
{apiKey: 'removedmykeyforpostinginSO'}
);
});
//defining controller
app.controller('myController', function ($scope, employees)
{
$scope.countries = employees.query();
});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myController">
<ul>
<li ng-repeat = "objCountry in countries" >
{{objCountry.name}}
</li>
</ul>
</div>
</body>
</html>
JSON from Mongolab
[ { "_id" : { "$oid" : "57044f95e4b0427faa38585f"} , "name" : "Lijo" , "age" : "30"} ]
From AngulaJS Documentation, the recommended way of declaring factories is:
angular.module('myModule', [])
.factory('serviceId', ['depService', function(depService) {
// ...
}])
I corrected my factory code as below, by injecting $resource:
app.factory('employees', function ($resource) {
return $resource('https://api.mlab.com/api/1/databases/humanresource/collections/Employees',
{apiKey: 'jJbAltZ26vuIrgzUQetXh480JM0q9k6p'}
);
});
References: