Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service guide doesnt work #754

Closed
MrOutput opened this issue Jul 25, 2016 · 5 comments
Closed

service guide doesnt work #754

MrOutput opened this issue Jul 25, 2016 · 5 comments
Labels

Comments

@MrOutput
Copy link

MrOutput commented Jul 25, 2016

I am not sure if the angular team had mixed ideas about services and factories. But the code and ideas mentioned in the services section don't add up, not even on angular's site. If service registration takes a constructor function as the second argument, and its suppose to be invoked as a constructor would, with the new keyword, then why can't I treat it as a true constructor passing in parameters?

Service

(function () {
    angular
        .module("App")
        .service("Person", Person);

    //angular wants my parameters to be injectables.
    function Person(name, age) {
        this.name = name;
        this.age = age;
    }
})();

Controller

(function () {
    angular
        .module("App")
        .controller("DemoController", DemoController);

    DemoController.$inject = ["Person"];

    function DemoController(Person) {
        var a = new Person("rafael", 22);// ERROR

        var Demo = this;

        Demo.title = "Demo";
    }
})();
@phpedinei
Copy link

phpedinei commented Jul 25, 2016

Use factory instead of service
Angular is singleton for factory and services...

(function() {

    angular
        .module('App')
        .factory('servicePerson', servicePerson);

    function servicePerson() {
        function Person(name, age) {
            this.name = name;
            this.age = age;
        }

        Person.prototype.alert = function() {
          alert('Name:' + this.name +' Age:'+ this.age);
        }

        var service = {
            newPerson : newPerson
        }

        return service;

        function newPerson(name, age){
            return new Person(name, age); 
        }
    }
}) ();

In controller

(function() {

    angular
        .module('App')
        .controller('DemoController', DemoController);

    DemoController.$inject = ['servicePerson'];

    function DemoController(servicePerson) {
        var a =  servicePerson.newPerson('rafael', 22);
        a.alert();
        var Demo = this;

        Demo.title = 'Demo';
    }
}) ();

@MrOutput
Copy link
Author

That's just not right friend.

@sava-vidakovic
Copy link

Work for me.
http://jsfiddle.net/sava/612phm6n/

@phpedinei
Copy link

@MrOutput, yeah, but what's is the best way?

@sava-vidakovic. for me too

@johnpapa
Copy link
Owner

johnpapa commented Sep 5, 2016

this is just how they work :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants