angularjs - 如何继承ui-router中的解析数据

标签 angularjs angular-ui-router

我这里遇到了一个棘手的情况。我的父状态和子状态都覆盖顶层的相同 ui View (index.html)。因此,当它从父级进入子状态时,范围会被破坏(我认为?)基本上父级有一个解析,该解析存储在 MetricData 属性中,我似乎无法从子级访问该属性,因为它不是嵌套的假设。有没有办法在子级中获取 metricdata 属性,而不必在子级中再次手动调用相同的 ajax 调用

父状态

     .state("home.metric", {
      url: "/category/:categoryId/metric/:metricId/name/:metricName",
      views: {

        'main@': {

          templateUrl:
            function (stateParams){
              //move this to a util function later
              var tempName = unescape(stateParams.metricName);
              tempName = tempName.replace(/\s/g, "-");

              return '../partials/slides/' + tempName + '.html';
            },
          resolve: {
            MetricData: ['MetricService', '$stateParams', 
              function(MetricService,    $stateParams){
                var data = { categoryId : $stateParams.categoryId, metricId : $stateParams.metricId};
                return MetricService.getMetricDetails(data);
              }]
          },
          controllerProvider: 
            function ($stateParams) {
              var tempName = unescape($stateParams.metricName);
              tempName = tempName.replace(/\s+/g, '');

              return tempName + 'Ctrl';
            }
        }
      }

    })

子状态

.state("home.metric.detail", {
      url: "/detailId/:detailId/detailName/:detailName",
      views: {

        'main@': {
          templateUrl:
            function ($stateParams){
              //move this to a util function later
              var tempName = unescape($stateParams.detailName);
              tempName = tempName.replace(/\s/g, "-");

              return '../partials/slides/' + tempName + '.html';
            },
          resolve: {
            DetailData: ['DetailService', '$stateParams',
              function(DetailService,      $stateParams){
                var data = { categoryId : $stateParams.categoryId, detailId : $stateParams.detailId};
                return DetailService.getDetails(data);
              }],
            // MetricData: ['MetricService', '$stateParams', 
            //   function(MetricService,    $stateParams){
            //     var data = { categoryId : $stateParams.categoryId, metricId : $stateParams.metricId};
            //     return MetricService.getMetricDetails(data);
            //   }]
          },
          controllerProvider: 
            function ($stateParams) {
              var tempName = unescape($stateParams.detailName);
              tempName = tempName.replace(/\s+/g, '');

              return tempName + 'Ctrl';
            }
        }

      }

    })

最佳答案

我。问题的答案:

... child since its not nested ... Is there a way to get that metricdata property in the child?

基于

What Do Child States Inherit From Parent States?

Child states DO inherit the following from parent states:

  • Resolved dependencies via resolve
  • Custom data properties

Nothing else is inherited (no controllers, templates, url, etc).

结合

Scope Inheritance by View Hierarchy Only

Keep in mind that scope properties only inherit down the state chain if the views of your states are nested. Inheritance of scope properties has nothing to do with the nesting of your states and everything to do with the nesting of your views (templates).

It is entirely possible that you have nested states whose templates populate ui-views at various non-nested locations within your site. In this scenario you cannot expect to access the scope variables of parent state views within the views of children states.

II. 虽然现在应该很清楚,但我们仍然需要找到一种方法解决:

... Is there a way to get that metricdata property in the child without having to manually call the same ajax call again in the child..

我想说,也有答案。例如

.. move the shared views/resolvers to the least common denominator. ..

例如我们可以像这个问答中那样做:Controlling order of operations with services and controllers ,参见plunker :

特殊的祖 parent /根状态:

$stateProvider
  .state('root', {
    abstract : true,
    // see controller def below
    controller : 'RootCtrl',
    // this is template, discussed below - very important
    template: '<div ui-view></div>',
    // resolve used only once, but for available for all child states
    resolve: {
      user: function (authService) {
          return authService.getUserDetails();
      }
    }
  }) 

将已解析的内容传递到 $scope(由每个子级继承)

.controller('RootCtrl', function($scope,user){
  $scope.user = user;
})

它被注入(inject)到我们的状态/ View 层次结构的顶部,任何子状态都会从中受益

// any previously root state will just use the above as a parent
.state('index', {
    url: '/',
    parent : 'root',

查看更多详情here并在working example中看到它

关于angularjs - 如何继承ui-router中的解析数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26513746/

相关文章:

angularjs - 如何在 AngularJS 中使用与 ngClass 指令的一种方式绑定(bind)?

javascript - 如何使用jQuery从 Angular outerHtml获取所选选项的innerText?

javascript - Angular-ui-router 嵌套 View

javascript - 无需重新加载 Controller 的子路由中的 UI-Router 和可选参数

javascript - Angular ui.router 设置祖先状态命名 View

testing - 使用 angularjs 模拟而不实际模拟任何东西

javascript - Angular.js 在 ng-repeat 中获取二维数组中最后一个元素的索引

javascript - Angularjs 根据 id 过滤嵌套 JSON 数据

javascript - AngularJS $locationChangeStart 事件取消路由导航

javascript - Angular在不同组件中显示搜索结果