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

I'm new to Angularjs and everything was going well until I've got this problem.I'm gett

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743948537a4534513.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信