angularjs指令中的ng控制器?

angularjs指令中的ng控制器?,angularjs,angularjs-directive,Angularjs,Angularjs Directive,在许多教程中,我看到控制器被传递到如下指令: .directive("mainRightPanel", function(){ return { templateUrl: "views/partials/panels/mainRightPanel.html", controller: function($http, $filter, $scope) { var info = this; $scope.author

在许多教程中,我看到控制器被传递到如下指令:

.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;
            });
          },
}
不过,我想换一种方式。我没有像上面那样编写javascript,而是遇到了从指令中调用控制器的问题,如下所示

<div ng-app="mainLeftPanelModule" ng-controller="leftPanelController" class="list-group ">


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

您可以将ng控制器放在ng应用程序中的任何位置,包括指令模板中。例如,这应该起作用:

.directive("mainRightPanel", function(){
    return {
    template: '<div ng-controller="myCtrl">The rest of your directive can go here<div>'
  }
}
指令(“mainRightPanel”,函数(){ 返回{ 模板:“指令的其余部分可以转到此处” } }
指令中的控制器为“自动调用”使用JU时,不需要使用指令ngController@Dalorzo谢谢,但我正在尝试查看是否可以从指令内调用控制器,而不必从指令定义链接控制器!您可以在任何地方使用ng控制器。即使在指令模板内也可以。您对此有问题吗?@rob you's对!我想我犯了一个错误,把ng应用程序和ng控制器放在同一个div上。如果你回答我的问题,我一定会把它标记为正确的,谢谢!