kliukovking
kliukovking

Reputation: 590

Calling parent method

I have some Factory:

app.factory('myFactory', function() {
  return {
    property1: "str",
    property2: "str",
    property3: "str",
    func: function() {/*....*/},
    childObj:{
      prop1: "str",
      prop2: "str",
      childFunc: function() {
         //....;
         func();    
      }
    }
  }
})

Can I call parent factory method inside child method?

Upvotes: 0

Views: 49

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

But can I call parent method inside child method inside Angularjs factory?

As you defined factory - no

app.factory('myFactory', function(){
   return{
    property1:"str",
    property2:"str",
    property3:"str",
    func:function(){
      return "fess";
    },
    childObj:{
       prop1:"str",
       prop2:"str",
       childFunc:function(){
          return func();    // here you will get error: func() is undefined
       }
    }
  }
 })

However this will work, when we create factory var:

app.factory('myFactory', function(){
   var factory = {
    property1:"str",
    property2:"str",
    property3:"str",
    func:function(){
      return "fess";
    },
    childObj:{
       prop1:"str",
       prop2:"str",
       childFunc:function(){
          return factory.func();    // <-- OK
       }
    }
  };

  return factory;
 })

Call:

console.log(myFactory.childObj.childFunc()); // fess

Demo Plunker

Upvotes: 1

Related Questions