【问题标题】:why can't angular js can't find the custom controller为什么angular js找不到自定义控制器
【发布时间】:2019-01-23 18:28:51
【问题描述】:

我在一个单独的文件中创建了一个自定义 angularjs 控制器,调用者也在一个单独的文件中。但由于某种原因,angularjs 给出了错误控制器未定义。以下是文件:

test_ctrl.js:

function test_ctrl($scope, $rootScope, apiSrvc, $mdDialog) {

    $scope.hide = function() {$mdDialog.hide();};
    $scope.cancel = function() {$mdDialog.cancel();};

  }

index.html:

<button id="testbutton" ng-click="openModal($event)">open modal</button>

index.js:

// Open the Modal // 
  $scope.openModal = function(ev) { 
    $mdDialog.show({
      controller: test_ctrl,  
      templateUrl: '/views/testview/testmodal.html',
      parent: angular.element($('.some_parent_class')),
      targetEvent: ev,
      clickOutsideToClose:true,
      locals: {},
      onRemoving: function (event, removePromise) {
      }
    });
  };
  //*/ 
});

testmodal.html:

<md-dialog>
    <md-dialog-content>
        testing content
    </md-dialog-content>
    <md-dialog-actions layout="row">
        <md-button ng-click="cancel()">Close</md-button>
    </md-dialog-actions>
</md-dialog>

可以总结一下这个过程,index.html 中的按钮是用来打开一个角度模态对话框的。因此,对于 index.html 中的按钮,我包含“ng-click="openModal($event)" 来触发角度事件以打开角度模态。我不知道我错过了什么或做错了什么,但在控制台中单击按钮时,我得到““test_ctrl”未在角度中定义...”错误。 我做错了什么?

编辑 好的另一种解决方法是将控制器定义函数包含在与 index.js 相同的文件中,但我真的很想在 openModal 参数中包含外部控制器文件的位置,就像您可以使用模态模板位置(模板网址:'/views/testview/testmodal.html')。控制器文件的位置有参数吗?

【问题讨论】:

  • 您是否将控制器置于全局范围内?还是您将其包含在一个模块中?
  • @georgeawg 我将控制器放在外部文件中,并让 openModal 角度方法调用它。有没有办法在 openModal 参数中定义控制器文件的位置?

标签: javascript angularjs twitter-bootstrap angularjs-controller


【解决方案1】:

我将控制器放在外部文件中并让 openModal 角度方法调用它。有没有办法在 openModal 参数中定义控制器文件的位置?

将控制器定义为模块的一部分:

angular.module("test",[])
.controller("test_ctrl",
  function test_ctrl($scope, $rootScope, apiSrvc, $mdDialog) {
    $scope.hide = function() {$mdDialog.hide();};
    $scope.cancel = function() {$mdDialog.cancel();};
})

在主模块中将其声明为依赖项:

angular.module("app",['test'])

在模态中,使用字符串调用:

  $scope.openModal = function(ev) { 
    $mdDialog.show({
      controller: ̶t̶e̶s̶t̶_̶c̶t̶r̶l̶,̶ "test_ctrl",  
      templateUrl: '/views/testview/testmodal.html',
      parent: angular.element($('.some_parent_class')),
      targetEvent: ev,
      clickOutsideToClose:true,
      locals: {},
      onRemoving: function (event, removePromise) {
      }
    });
  };

欲了解更多信息,请参阅AngularJS Developer Guide - Modules


当将其定义为模块的一部分时,我可以将该代码放在外部文件中,对吗?没有明确告诉角度它在哪里或将它包含在脚本标签中对吗?

包含模块的文件可以按任何顺序加载(在angular.js 之后)。当框架找到ng-app 指令并构建应用程序时,将整理出依赖关系。

更多信息请见John Papa AngularJS Style Guide - Modules

【讨论】:

  • 将其定义为模块的一部分时,我可以将该代码放在外部文件中,对吗?没有明确告诉角度它在哪里或将它包含在脚本标签中吗?
  • 有没有办法找出所有已定义的全局 Angular js 模块?
  • 这应该是一个新问题。
【解决方案2】:

控制器函数定义需要在index.js加载之前定义,所以在index.html中需要在index.js之前加载test_ctrl.js

更简洁的替代方法是将控制器函数导入index.js

【讨论】:

  • 等你告诉我 angular 找不到?我可以在 index.js 中定义的 openModal 函数中以某种方式定义该位置吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多