user2853731
user2853731

Reputation:

Unknown provider error in Angular js

While running the following code the error "Error: [$injector:unpr] Unknown provider: ItemsProvider <- Items" occurs.

HTML code:

<div ng-controller="ShoppingController">
<table>
    <tr>
        <td>{{items.title}}</td>
        <td>{{items.price}}</td>
    </tr>
</table>

Javascript code :

var angModule = angular.module('ShoppingModule',[]);
angModule.factory('Items', function() {
    var items = {};
    items.query = function() {
        return [{title :'JW',price:100}];
    }
        return items;

}) ;
function ShoppingController($scope, Items) {
    $scope.items = Items.query();
}

How to fix this

Upvotes: 0

Views: 2024

Answers (1)

dnc253
dnc253

Reputation: 40327

Your factory method needs to return the object that will be injected. With the code above, it isn't returning anything. Should be:

var angModule = angular.module('ShoppingModule',[]);
angModule.factory('Items', function() {
    var items = {};
    items.query = function() {
        return [{title :'JW',price:100}];
    }

    return items;// <----- Add this
});
function ShoppingController($scope, Items) {
    $scope.items = Items.query();
}

Upvotes: 1

Related Questions