0

in AngularJs if you invoke service method:

app.service('nameService', function(){
this.Service = function (){console.log('hello')}
}

then you can use this service (object) : nameService.Service() so my question is how i can write function that have two arguments(name, function)

function service ( name, function ){
// how angular declare object with that value of first argument ? 
}

i'm ask how to write function like service(name,fn){} ?

2
  • You need to inject the service in a controller to use it. Is this what you mean or something else Commented Sep 7, 2016 at 11:40
  • no i not talk about angularJs , i try to use js and function cunstractor to build function like service in angular. when you invoke service function in angular js , what the basic what happend Commented Sep 7, 2016 at 11:55

2 Answers 2

1

AngularJs use Dependency Injection to inject service.

"service" method is available on angular object because is on his Prototype.

angular.prototype.service = ...

you can read more about prototype inheritance, MDN - Inheritance and the prototype chain

Sign up to request clarification or add additional context in comments.

Comments

1

you can use bracket notation , to get arguments value .

function service (name, fn){

    var createService = function(){
        return new fn()
    }

    this[arguments[0]] = func()
}

service('nameService',function(){
    //here you can use 'this' because it's function constructor

    this.getLog = function (){
        console.log('hello')
    }

})


//and you can user it like you  want nameService.getLog() 
//Console: hello

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.