3

How can I disable a child state in angular UI-router from inheriting its parent state's data?

For example, I have a parent and child state as follows:

$stateProvider.state('parent', {
      data:{
         customData1:  "Hello",
      }
   })
   .state('parent.child', {
      data:{

      }
   });

Here the child state(parent.child) inherits the data(customData1) of its parent. How can I disable it from inheriting parent's data?

1
  • I'd like to be able to disable this inheritance too. I keep data specific to each state and need always to remember to overwrite it in children. Not very efficient. Commented Apr 6, 2015 at 4:21

1 Answer 1

0

While I am not sure why would you like to do that.. you can hide the parent implementation:

Inherited Custom Data

Child states will inherit data properties from parent state(s), which they can overwrite.

$stateProvider.state('parent', {
      data:{
         customData1:  "Hello",
         customData2:  "World!"
      }
   })
   .state('parent.child', {
      data:{
         // customData1 inherited from 'parent'
         // but we'll overwrite customData2
         customData2:  "UI-Router!"
      }
   });

$rootScope.$on('$stateChangeStart', function(event, toState){ 
    var greeting = toState.data.customData1 + " " + toState.data.customData2;
    console.log(greeting);

    // Would print "Hello World!" when 'parent' is activated
    // Would print "Hello UI-Router!" when 'parent.child' is activated
})
Sign up to request clarification or add additional context in comments.

6 Comments

Here, the parent.child state will inherit the customData1 from the parent. I don't want that to happen. I want the parent.child.data.customData1 to be null.(or undefined).In other words, I want to disable the data inheritance from parent to child state.
I can't do that. Thats why I mentioned I want to disable the inheritance of data :)
Stop inheritance of data is impossible. The above answer is the soltuion. (But from my perspective, just my point of view - that could not be issue. You should be able to handle your state definitions. It is in your hands. If you need different setting in your child, then use data : { subChildSetting : {...}}) Sorry for not having "the answer". Good luck with UI-Router anyhow
Thank you.It is also ok if I come to know whether the data is inherited or custom defined. I had posted another question here. Can you please let me know if that is possible?
:) but this test will fail if I explicitly mention the same value in parent as well as child state :) ie,$stateProvider.state('parent', { data:{ customData1: "Hello"; } }) .state('parent.child', { data:{ // customData1 inherited from 'parent' // but we'll overwrite customData2 customData1: "Hello"; } });
|

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.