最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can not use services AngularJS - 'Cannot read property 'prototype' of undefined' -

programmeradmin2浏览0评论

I'm new to Angularjs and everything was going well until I've got this problem.
I'm getting this error as soon as I load the page when I try to use the service inside a controller :

TypeError: Cannot read property 'prototype' of undefined
at Object.instantiate (.3.5/angular.js:4145:82)
at Object.<anonymous> (.3.5/angular.js:4007:24)
>>at Object.invoke (.3.5/angular.js:4138:17)
>>at Object.enforcedReturnValue [as $get] (.3.5/angular.js:3991:37)
>>at Object.invoke (.3.5/angular.js:4138:17)
>>at .3.5/angular.js:3956:37
>>at getService (.3.5/angular.js:4097:39)
>>at Object.invoke (.3.5/angular.js:4129:13)
>>at extend.instance (.3.5/angular.js:8376:21)
>>at .3.5/angular.js:7624:13

This is my code:

var app = angular.module('myApp', []);

app.service('Service'), function () {
    var category = { name: 'Test' };

    return {
        set: function (newObj) {
            category.name = newObj;
        },
        get: function () {
            return category;
        }
    };
};

app.controller('MainController', function (Service, $scope) {
    $scope.save = function() {
        Service.set($scope.search);
    };

    $scope.message = Service.get();
});

Please help

I'm new to Angularjs and everything was going well until I've got this problem.
I'm getting this error as soon as I load the page when I try to use the service inside a controller :

TypeError: Cannot read property 'prototype' of undefined
at Object.instantiate (https://code.angularjs/1.3.5/angular.js:4145:82)
at Object.<anonymous> (https://code.angularjs/1.3.5/angular.js:4007:24)
>>at Object.invoke (https://code.angularjs/1.3.5/angular.js:4138:17)
>>at Object.enforcedReturnValue [as $get] (https://code.angularjs/1.3.5/angular.js:3991:37)
>>at Object.invoke (https://code.angularjs/1.3.5/angular.js:4138:17)
>>at https://code.angularjs/1.3.5/angular.js:3956:37
>>at getService (https://code.angularjs/1.3.5/angular.js:4097:39)
>>at Object.invoke (https://code.angularjs/1.3.5/angular.js:4129:13)
>>at extend.instance (https://code.angularjs/1.3.5/angular.js:8376:21)
>>at https://code.angularjs/1.3.5/angular.js:7624:13

This is my code:

var app = angular.module('myApp', []);

app.service('Service'), function () {
    var category = { name: 'Test' };

    return {
        set: function (newObj) {
            category.name = newObj;
        },
        get: function () {
            return category;
        }
    };
};

app.controller('MainController', function (Service, $scope) {
    $scope.save = function() {
        Service.set($scope.search);
    };

    $scope.message = Service.get();
});

Please help

Share Improve this question asked Dec 26, 2014 at 7:42 Martin HultmanMartin Hultman 431 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You are not declaring your service properly. You have an addition closing ) in service declaration.

Line:

app.service('Service'), function () {

Should Be:

app.service('Service', function () {

In case you're using TypeScript you have to make sure the class definition is above the angular.module defintion.

Wrong:

angular.module('app').service('Service', Service);

class Service {

}

Correct:

class Service {

}

angular.module('app').service('Service', Service);
发布评论

评论列表(0)

  1. 暂无评论