angularjs中的嵌套视图,每个视图都有独立的控制器

时间:2015-03-16 09:11:54

标签: javascript angularjs

我有观点

<section ng-controller="ParentController">
<!-- some contents -->
   <my-directive controller="ChildController"></my-directive>
</section>

脚本

    var app = angular.module('section', []);
    app.controller('ParentController', ['$scope', function($scope){
      //
    }]);
app.controller('ChildController', ['$scope', function($scope){
      //
    }]);

app.directive('myDirective', function(){
  return {
    template : function(ele, args){
       return '<section ng-controller="+args.controller+" ></section>';
    },
    restrict  : 'E'
  }
});

渲染完成后,'ChildController'不是函数,未定义'

为什么会这样?

1 个答案:

答案 0 :(得分:0)

尝试这样做:

&#13;
&#13;
var app = angular.module('app', []);
app.controller('ParentController', ['$scope', function($scope){
      //
    }]);
app.controller('ChildController', ['$scope', function($scope){
      //
    }]);
&#13;
&#13;
&#13;

相关问题