2

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.org/1.3.5/angular.js:4145:82)
at Object.<anonymous> (https://code.angularjs.org/1.3.5/angular.js:4007:24)
>>at Object.invoke (https://code.angularjs.org/1.3.5/angular.js:4138:17)
>>at Object.enforcedReturnValue [as $get] (https://code.angularjs.org/1.3.5/angular.js:3991:37)
>>at Object.invoke (https://code.angularjs.org/1.3.5/angular.js:4138:17)
>>at https://code.angularjs.org/1.3.5/angular.js:3956:37
>>at getService (https://code.angularjs.org/1.3.5/angular.js:4097:39)
>>at Object.invoke (https://code.angularjs.org/1.3.5/angular.js:4129:13)
>>at extend.instance (https://code.angularjs.org/1.3.5/angular.js:8376:21)
>>at https://code.angularjs.org/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

2 Answers 2

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 () {
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! You get blind when you look at some code for more then 5h :)
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.