【问题标题】:Updating resolved objects in ui.router parent states更新 ui.router 父状态中的已解析对象
【发布时间】:2014-10-07 09:18:29
【问题描述】:

我的问题有两个:

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

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?
}

提前致谢

【问题讨论】:

    标签: angularjs angular-ui-router


    【解决方案1】:

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

    让我们拥有这些状态

    $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',
      })
    

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

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

    对于这个例子,这是共享模板:

    <div>
      <h3>{{state.current.name}}</3>
    
      x <input ng-model="data.objectX.x"/>
      y <input ng-model="data.objectX.y"/>
    </div>
    

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

    Scope Inheritance by View Hierarchy Only

    检查该示例的实际操作here。如果您需要更多详细信息(比上面的链接,请查看此Q&A

    【讨论】:

    • 就是这样。但有点不同,主要是结合你和@Chris T 的答案。这是更新的代码plnkr.co/edit/wFeyMk3FD6kHH1HcRCFa?p=preview。不知何故,我认为由于解析对象是一个承诺,我无法更新它。但似乎它得到了“解决”,并且引用始终是一个对象。我知道这应该很明显:-)
    【解决方案2】:

    您可以将其存储在服务中。

    .service("myService", function($q) { 
      // the actual data is stored in a closure variable
      var data = undefined;
      return { 
        getPromise: function() { // promise for some data
          if (data === undefined) // nothing set yet, go fetch it
            return $http.get('resourceurl').then(function(value) { data = value; return data; });
          else
            return $q.when(data); // already set, just wrap in a promise.
        },
        getData: function() { return data; }, // get current data (not wrapped)
        setData: function(newDataVal) { data = newDataVal; } // reset current data
      }
    })
    
    // `parent` wont' enter until getPromise() is resolved. 
    .state('parent', resolve:{objectX: function(myService) { return myService.getPromise(); } });
    
    .controller('childstateCtrl', $scope, myService) {
        $scope.data.objectX = myService.getData();
        $scope.someEvent = function(someData){ 
          myService.setData(someData);
        }
    }
    
    .controller('otherChildCtrl', $scope, myService){
        // how to get the updated objectX?
        var data = myService.getData();
    }
    

    【讨论】:

    • 嗨,克里斯,你的回答部分是我想要的,但 Radim 的范围继承正是我想要的(还有一个已解决的承诺可以更新的概念 [是的,愚蠢的 :-) ]),很抱歉,但我会给他答案
    • 应该用 .value() 解决,这样他们就可以通过服务共享对象,这是正确的方式,因为共享范围不是正确的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2018-09-08
    • 2019-01-28
    • 1970-01-01
    • 2016-10-09
    • 2020-05-02
    • 2020-07-01
    相关资源
    最近更新 更多