在ui.router父状态中更新已解析的对象

Ric*_*mes 7 angularjs angular-ui-router

我的问题有两个:

1 - 我有一个父状态和几个子状态使用ui.router,有一个对象(存储在mongodb中)在所有状态下都需要,但它会被所有状态更新.在这种情况下,使用父状态的resolve选项填充对象是否有意义?

2 - 如果这是执行此操作的正确方法,如何从每个状态更新该对象的"引用"(由ui.router创建的模拟服务注入器).

为了帮助解释他是这个想法的一个例子(很多代码被忽略)

.state('parent',resolve:{objectX:return x.promise;},...);

.controller('childstateCtrl',$scope,objectX){
    $scope.data.objectX = objectX;
    $scope.someEvent =function(){ 
    // something updates objectX on the scope
    }
}

.controller('otherChildCtrl',$scope,objectX){
    // how to get the updated objectX?
}
Run Code Online (Sandbox Code Playgroud)

提前致谢

Rad*_*ler 4

不完全确定我是否能看到问题出在哪里......但如果您正在寻找一种如何共享对更新参考的访问权限的方法,那应该很容易。有一个例子

让我们拥有这些状态

$stateProvider
  .state('root', {
    abstract: true,
    template: '<div ui-view></div>',
    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',
  })
Run Code Online (Sandbox Code Playgroud)

仅使用一个控制器(对于根状态):

.controller('rootController', function($scope, objectX){
  $scope.data = { objectX: objectX };
})
Run Code Online (Sandbox Code Playgroud)

对于此示例,这是共享模板:

<div>
  <h3>{{state.current.name}}</3>

  x <input ng-model="data.objectX.x"/>
  y <input ng-model="data.objectX.y"/>
</div>
Run Code Online (Sandbox Code Playgroud)

因此,在这种情况下,父级(root)已将对象数据注入到 $scope 中。然后按照此处所述继承该引用:

仅通过视图层次结构继承范围

请在此处查看该示例的实际运行情况。如果您需要更多详细信息(比上面的链接更多,请查看此问答