How to disable data inheritance of angular ui router states

前端 未结 1 713
渐次进展
渐次进展 2021-01-20 03:55

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:<

相关标签:
1条回答
  • 2021-01-20 04:39

    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
    })
    
    0 讨论(0)
提交回复
热议问题