-1

" Error: [ng:areq] Argument 'curController' is not a function, got string当我尝试将变量放入 ng-controller 参数时,我收到一条消息

    <div  ng-include="templates[selected-1]" ng-controller='curController'>

控制器

  for(i=0;i<$scope.cours.length;i++){
    switch($scope.cours[i].type){
        case "c1":
            $scope.controllers[i]="cours1Display"
            $scope.templates[i]="views/cours/cours1.html"
            break;
        case "c2":
            $scope.controllers[i]="cours2Display"
            $scope.templates[i]="views/cours/cours2.html"
            break;
        case "c3":
            $scope.controllers[i]="cours3Display"
            $scope.templates[i]="views/cours/cours3.html"
            break;
        case "c4":
            $scope.controllers[i]="cours4Display"
            $scope.templates[i]="views/cours/cours4.html"
            break;
        case "exo1":
            $scope.controllers[i]="exo1Display")
            $scope.templates[i]="views/exos/exo1Decorator.html"
            break;
        case "exo2":
            $scope.controllers[i]="exo2Display"
            $scope.templates[i]="views/exos/exo2Decorator.html"
            break;
        case "exo3":
            $scope.controllers[i]="exo3Display"
            $scope.templates[i]="views/exos/exo3Decorator.html"
            break;
       }

curController =   $scope.controllers[0]

$scope.pageChanged=function(){
    alert($scope.selected)
    curController =   $scope.controllers[$scope.selected-1]
}
4

1 回答 1

0

您是否尝试使用括号?当然,'curController' 应该分配给 $scope.curController。

<div  ng-include="templates[selected-1]" ng-controller='{{curController}}'>

似乎 Angular 不支持动态控制器名称,但您可以编写自己的指令来设置自定义控制器。

angular.module('app', [])
  .directive('myController', function($controller) {

    return {
      restrict: 'A',
      priority: 600,
      scope: true,
      link: function(scope, element, attr) {
        console.log(scope, element, attr);
         var locals = {
              $scope: scope,
              $element: element,
              $attrs: attr,              
            };
         element.data('$myController', $controller(attr['myController'], locals));
      }
    };

  })
  .controller('CustomController', function($scope) {
    console.log($scope);
    $scope.value = 'My custom value in my custom controller.'
  })

;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js"></script>
<div ng-app="app" ng-init="curController='CustomController'">
  <div my-controller="{{curController}}">value={{value}}</div>

于 2015-04-30T08:55:15.447 回答