简体   繁体   English

如何为一个模板提供一个控制器的多个实例

[英]How can I have multiple instances of one controller for multiple templates

Trying to use a reusable controller for a generic template 尝试对通用模板使用可重用的控制器

html HTML

<div class="row col-lg-10 pull-right">

   <div ng-init="catId = 1" ui-view="firstQuadrant"></div>
   <div ng-init="catId = 2" ui-view="secondQuadrant"></div>

</div>

<div class="row col-lg-10 pull-right">

   <div ng-init="catId = 3" ui-view="thirdQuadrant"></div>
   <div ng-init="catId = 4" ui-view="fourthQuadrant"></div>

</div>

code snippet from my views object in $stateProvider : 我在$stateProvider views对象的代码片段:

views : {
    'firstQuadrant@home': {
      templateUrl: "../partials/quadrant/quadrant.html",
      controller: "QuadrantCtrl"
    },
    'secondQuadrant@home': {
      templateUrl: "../partials/quadrant/quadrant.html",
      controller: "QuadrantCtrl"
    },
    'thirdQuadrant@home': {
      templateUrl: "../partials/quadrant/quadrant.html",
      controller: "QuadrantCtrl"
    },

    'fourthQuadrant@home': {
      templateUrl: "../partials/quadrant/quadrant.html",
      controller: "QuadrantCtrl"
    }
}

controller code 控制器代码

.controller('QuadrantCtrl', ['$scope', '$rootScope', 'categories', 'utils', 
   function ($scope, $rootScope, categories, utils) {

  $scope.$watch('catId', function () {
      console($scope.catId); 
      $scope.categories = categories;
      $scope.name = "It works! weee";
      $scope.score = 99;
      $scope.items = utils.findById($scope.categories, $scope.catId);
  });     

}]);

It only seems to use the last controller being instantiated (catId = 4) how can I have 4 isolated scopes? 它似乎只使用实例化的最后一个控制器(catId = 4),我怎样才能拥有4个隔离的作用域? Do I have to use directives instead? 我是否必须使用指令?

Your scenario should work (not sure if this is good design) . 您的方案应该可以工作(不确定这是否是好的设计) There is a working plunker 有一个正在工作的矮人

But we have to move the switch catId from ng-init into state defintion. 但是我们必须将开关catId从ng-init移到状态定义中。 Into resolve 化解

If the states are defined like this: 如果状态是这样定义的:

// home
$stateProvider
  .state('home', {
    url: '/home',
    templateUrl: 'tpl.layout.html',
    controller : "rootController",
  })

the child state with multi-views 具有多视图的子状态

  .state('child', {
    parent: "home",
    url: '/child',
    templateUrl: 'tpl.example.html',
    views : {
       'firstQuadrant@home': {
          templateUrl: "tpl.quadrant.html",
          controller: "QuadrantCtrl",
          resolve: { catId : function(){ return 1 } },
        },
        'secondQuadrant@home': {
          templateUrl: "tpl.quadrant.html",
          controller: "QuadrantCtrl",
          resolve: { catId : function(){ return 2 } },
        },

        'thirdQuadrant@home': {
          templateUrl: "tpl.quadrant.html",
          controller: "QuadrantCtrl",
          resolve: { catId : function(){ return 3 } },
        },

        'fourthQuadrant@home': {
          templateUrl: "tpl.quadrant.html",
          controller: "QuadrantCtrl",
          resolve: { catId : function(){ return 4 } },
        }
    }
  })

And simplified controller creates random number in the scope 简化的控制器在范围内创建随机数

.controller('QuadrantCtrl', ['$scope', '$rootScope', 'catId' 
, function ($scope, $rootScope, catId) {

      $scope.catId = catId;

      console.log($scope.catId); 

      $scope.random = Math.random() * 100;

}])

Each view is then independent with its own instance of controller and $scope 然后,每个视图都独立于其自己的controller和$ scope实例

Check it here 在这里检查

Then we can see results like this 然后我们可以看到这样的结果

quadrant 象限
random number in the scope: 32.40865177940577 catId: 1 范围内的随机数:32.40865177940577 catId:1

quadrant 象限
random number in the scope: 17.18798188958317 catId: 2 范围内的随机数:17.18798188958317 catId:2

quadrant 象限
random number in the scope: 76.22438217513263 catId: 3 范围内的随机数:76.22438217513263 catId:3

quadrant 象限
random number in the scope: 41.46456739399582 catId: 4 范围内的随机数:41.46456739399582 catId:4

if the quadrant template is: 如果象限模板是:

<h4>quadrant</h4>
<p>random number in the scope: {{random}}</p>
<p>catId: {{catId}}</p>

All that is strictly following the documentation: 所有严格遵循文档的内容:

Multiple Named Views 多个命名视图

The working example with above stuff 上述内容的工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM