Updating resolved objects in ui.router parent states

前端 未结 2 1073
有刺的猬
有刺的猬 2020-12-07 01:59

My question is two fold:

1 - I have a parent state and several child states using ui.router, there\'s one object (stored in mongodb) that is need in all states, but

2条回答
  •  自闭症患者
    2020-12-07 02:42

    Not fully sure if I can see where is the issue... but if you are searching for a way how to share access to updated reference, it should be easy. There is an example

    Let's have these states

    $stateProvider
      .state('root', {
        abstract: true,
        template: '
    ', resolve: {objectX : function() { return {x : 'x', y : 'y'};}}, controller: 'rootController', }) .state('home', { parent: "root", url: '/home', templateUrl: 'tpl.example.html', }) .state('search', { parent: "root", url: '/search', templateUrl: 'tpl.example.html', }) .state('index', { parent: "root", url: '/index', templateUrl: 'tpl.example.html', })

    Working with only one controller (for a root state):

    .controller('rootController', function($scope, objectX){
      $scope.data = { objectX: objectX };
    })
    

    And for this example, this is shared template:

    {{state.current.name}} x y

    So, in this scenario, parent (root) has injected an object data into $scope. That reference is then inherit as described here:

    Scope Inheritance by View Hierarchy Only

    Check that example in action here. If you need more details (than in the link above, check this Q&A)

提交回复
热议问题