在angularjs指令中的ng-controller?

Jef*_*fLL 3 angularjs angularjs-directive

在许多教程中,我已经看到控制器被传递到这样的指令中:

.directive("mainRightPanel", function(){
  return {
        templateUrl: "views/partials/panels/mainRightPanel.html",
        controller: function($http, $filter, $scope) {
            var info = this;
            $scope.authorWorks;
            $http.get('test/book_data.json').success(function(data){
                $scope.authorWorks= data;
            });
          },
}
Run Code Online (Sandbox Code Playgroud)

但是,我想以不同的方式做到这一点.而不是像上面那样编写javascript,我在指令中调用控制器时遇到了问题,就像这样

<div ng-app="mainLeftPanelModule" ng-controller="leftPanelController" class="list-group ">
Run Code Online (Sandbox Code Playgroud)

这可能吗?如果是这样,以这种方式连接控制器有什么问题吗?

rob*_*rob 5

您可以将ng-controller放在ng-app内的任何位置,包括指令模板内.例如,这应该工作:

.directive("mainRightPanel", function(){
    return {
    template: '<div ng-controller="myCtrl">The rest of your directive can go here<div>'
  }
}
Run Code Online (Sandbox Code Playgroud)