简体   繁体   English

AngularJS,变量中的嵌套控制器

[英]AngularJS, nested controllers from variable

Generally, what I want to do, is to initialize nested ng-controller inside ng-repeat using variable. 通常,我想做的是使用变量在ng-repeat初始化嵌套的ng-controller

JSFiddle JSFiddle

JS JS

angular.module('app',[])
.controller('main',function($scope){
    angular.extend($scope,{
        name:'Parent Controller',
        items:[
            {name:'nested2'},
            {name:'nested1'}
            ]
    });
})
.controller('nested1',function($scope){
    $scope.name = "Name1";
})
.controller('nested2',function($scope){
    $scope.name = "Name2";
});

I want this: 我要这个:

<div ng-controller="main" ng-app='app'>
    Nested: {{name}}
    <div ng-controller="nested1">{{name}}</div>
    <div ng-controller="nested2">{{name}}</div>
</div>

to become to something like this: 变成这样的东西:

<div ng-controller="main">
    Nested: {{name}}
    <div ng-repeat="item in items">
        <div ng-controller="item.name">{{name}}</div>
    </div>
</div>

Problem: it does not work this way. 问题:这种方式不起作用。 Neither it works any other way, that I've tried after google'ing for an hour or so. 谷歌搜索一个小时左右后,我尝试了其他两种方法。

Is there any "legal" and nice way to achieve that at all? 是否有任何“合法”的好方法来实现这一目标?

There isn't a real way,using angular features it at this point, i suppose. 我想目前还没有一种真正的方法,就是使用角度特征。 You could create a directive and use un-documented dynamic controller feature controller:'@', name:'controllerName' . 您可以创建指令并使用未记录的动态控制器功能controller:'@', name:'controllerName' But this approach will not evaluate bindings or expressions that provide the controller name. 但是这种方法不会评估提供控制器名称的绑定或表达式。 What i can think of is a hack by instantiating a controller provided and setting it to the element. 我能想到的是通过实例化提供的控制器并将其设置为元素来实现。

Example:- 例:-

.directive('dynController', function($controller){
  return {
    scope:true, //create a child scope
    link:function(scope, elm, attrs){
      var ctrl =scope.$eval(attrs.dynController); //Get the controller
      ///Instantiate and set the scope
      $controller(ctrl, {$scope:scope})

     //Or you could so this  well
     //elm.data('$ngControllerController', $controller(ctrl, {$scope:scope}) ); 
    }
  }
});

And in your view:- 并且在您看来:-

  <div ng-controller="main">
    <div ng-repeat="item in items">
    Nested:
          <div dyn-controller="item.name" ng-click="test()">{{name}}</div>
    </div>
  </div>

Demo 演示版

Note that i have changed the position of ng-controller from the element that does ng-repeat, since ng-repeat ( 1000 ) has higher priority than ng-controller ( 500 ), ng-repeat's scope will prevail and you end up not repeating anything. 请注意,我已从执行ng-repeat的元素更改了ng-controller的位置,因为ng-repeat( 1000 )的优先级高于ng-controller( 500 ),所以ng-repeat的作用域将占上风,并且最终您不会重复任何东西。

While looking at it 看着它

Invested more couple hours into it, inspecting angular's sources etc. 在其中花费了几个小时,检查了Angle的来源等。

The expression, given to ng-controller , is not being evaluated at all. 给出给ng-controller的表达式根本没有被求值。

Here is best approach I've found: 这是我找到的最佳方法:

HTML: HTML:

    <div ng-controller="main">
        Nested: {{name}}
        <div ng-repeat="item in items">
            <div ng-controller="nested" ng-init="init(item)">{{name}}</div>
        </div>
    </div>

JS: JS:

angular.module('myApp', [])
    .controller('main', function ($scope, $controller) {
        angular.extend($scope, {
            name: 'Parent Controller',
            items: [
                {name: 'nested2'},
                {name: 'nested1'}
            ]
        });
    })
    .controller('nested', function ($scope, $controller) {
        angular.extend($scope, {
            init: function (item) {
                $controller(item.name, {'$scope': $scope});
            }
        });
    })
    .controller('nested1', function ($scope) {
        $scope.name = 'test1';
    })
    .controller('nested2', function ($scope) {
        $scope.name = 'test2';
    });

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

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