Angular js中的未知提供程序错误

时间:2013-11-26 03:57:43

标签: javascript html angularjs

运行以下代码时出现错误“错误:[$ injector:unpr]未知提供程序:ItemsProvider< - Items”出现。

HTML code:

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

Javascript代码:

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();
}

如何解决此问题

1 个答案:

答案 0 :(得分:1)

您的工厂方法需要返回将要注入的对象。使用上面的代码,它不会返回任何内容。应该是:

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();
}