无法读取属性' index'未定义的

时间:2016-12-27 08:04:11

标签: javascript ruby-on-rails angularjs angularjs-resource

我是Angular的新手,并试图在我的Rails应用程序上使用它。但是我收到了以下错误。

  

TypeError:无法读取属性' index'未定义的

app.js代码

angular.module('ticket', ['ngResource', 'ui.router', 'templates'])
.controller('AppCtrl', ['$rootScope', function($rootScope) {
    var _this = this;
    return $rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
        return $rootScope.pageTitle = $route.current.title;
    });
}])
.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'home/_home.html',
            controller: 'MainCtrl'
        })
        .state('members', {
            url: '/members',
            templateUrl: 'members/_members.html',
            controller: 'MemberCtrl'
        })
        .state('guards', {
            url: '/guards',
            templateUrl: 'guards/_guards.html',
            controller: 'GuardCtrl'
        })
        .state('tickets', {
            url: '/tickets',
            templateUrl: 'tickets/_tickets.html',
            controller: 'TicketCtrl'
        });
    $urlRouterProvider.otherwise('home')
}]);

memberCtrl.js代码(内部成员目录)

angular.module('ticket')
.factory('Member', ['$resource', function($resource) {
    return $resource("/members/:id", {id: "@id"}, {
        get: { method: 'GET'},
        save: { method: 'POST'},
        index: { method: 'GET', isArray: true},
        remove: { method: 'DELETE'},
        "delete": { method: 'DELETE'},
        update: { method: 'PUT'}
    });
}])
.controller('MemberCtrl', ['$scope', function($scope, Member) {
    console.log(Member.index());
    $scope.members = Member.index();
}]);

1 个答案:

答案 0 :(得分:6)

您需要在控制器中正确注入依赖项Member

.controller('MemberCtrl', ['$scope', 'Member', function($scope, Member) {
                                    //^^^^^^^^^^

而不是

.controller('MemberCtrl', ['$scope', function($scope, Member) {