如何使用不同的数据多次将数据解析为嵌套视图

时间:2015-10-07 22:52:04

标签: javascript html json angularjs angular-ui-router

我正在使用AngularJS框架(以及它的ui-router),我很难让我的数据以我想要的方式解析。我将在下面举例说明我要做的事情:



$stateProvider
  .state('home',{
    views: {
      'test': {
        templateUrl: 'test.html',
        resolve: {
          config: function() {
            var result = /*service call that returns json*/
            return result;
          }
        }
        controller: function($scope, config){
          console.log(config);
        }
      },
      'test': {
        templateUrl: 'test.html',
        resolve: {
          config: function() {
            var result = /*service call that returns DIFFERENT json*/
            return result;
          }
        }
        controller: function($scope, config){
          console.log(config);
        }
      }
    }
  })

<div ui-view="test">
<div ui-view="test">
&#13;
&#13;
&#13;

是否有任何方式可以无条件地分配和注入“配置”。进入控制器,以便它包含其各自的服务调用返回的json?这里的想法是我想要相同的模板,但不同的配置进入它们(作用于特定的视图)。现在,config只包含已执行的最后一个解析。

我希望我不要混淆。如果您需要任何澄清,请告诉我......非常感谢!

1 个答案:

答案 0 :(得分:1)

视图需要以不同的方式命名,结果也是如此。对它们使用相同的模板不是问题。最后,决心必须与国家相关,而不是观点。

&#13;
&#13;
$stateProvider
  .state('home',{
    views: {
      'test': {
        templateUrl: 'test.html',
        controller: function($scope, config){
          console.log(config);
        }
      },
      'test2': {
        templateUrl: 'test.html',
        controller: function($scope, config2){
          console.log(config2);
        }
      }
    },
    resolve: {
      config: function() {
        var result = /*service call that returns json*/
        return result;
      },
      config2: function() {
        var result = /*service call that returns DIFFERENT json*/
        return result;
      }
    }
  })
&#13;
<div ui-view="test">
<div ui-view="test2">
&#13;
&#13;
&#13;