从View调用Controller Method时出错 - AngularJS

时间:2015-01-06 03:43:01

标签: javascript json angularjs

我从视图中调用Controller Method getListCategories()时遇到此错误。

  

TypeError:在非对象上调用Object.keys       在Function.keys(本机)       在扩展(app / bower_components / angular / angular.js:412:25)       在Function.angular.module.provider。$ get.Resource。(匿名函数)(app / bower_components / angular-resource / angular-resource.js:575:15)       在Scope。$ scope.getListCategories(app / shoppinglists / shoppinglists.js:17:69)       at $ parseFunctionCall(app / bower_components / angular / angular.js:12332:18)       在Object.expressionInputWatch(app / bower_components / angular / angular.js:12735:31)       在Scope。$ get.Scope。$ digest(app / bower_components / angular / angular.js:14217:40)       在Scope。$ get.Scope。$ apply(app / bower_components / angular / angular.js:14488:24)       完成后(app / bower_components / angular / angular.js:9646:47)       at completeRequest(app / bower_components / angular / angular.js:9836:7)(22:31:11:425 |错误)

我不确定这是否是最好的方法,所以任何建议都非常受欢迎。

服务

shoppingListService.factory('ShoppingLists', function ($resource) {
var factory = {
    getShoppingLists: $resource('http://localhost:8080/ShoppingListBackend/webresources/shoppinglists', {}, {
        all: {method: 'GET', isArray: true}
    }),
    getCategories : $resource('http://localhost:8080/ShoppingListBackend/webresources/shoppinglists/getcategories/:id', {id: '@id'}, {
        byId: {method: 'GET', isArray: true}
    })
};
return factory; });

控制器

.controller('ShoppingListController', function ($scope, ShoppingLists) {
    $scope.shoppinglists = ShoppingLists.getShoppingLists.all();

    $scope.getListCategories = function (id) {
        $scope.listCategories = ShoppingLists.getCategories.byId(id);
    };
});

查看

<div id="card" ng-controller="ShoppingListController" ng-repeat="list in shoppinglists">
    <div id="card_title">{{list.title}}</div>
<div id="card_subtitle"><span>{{getListCategories(list.id)}}</span></div>
<div id="card_body">
    <input type="checkbox" id="item_checked">
    <label for="item_checked">Toilet Paper</label>
</div>

0 个答案:

没有答案