导入模块中的Angular控制器无法工作

Angular controller from imported module isn't working

本文关键字:工作 控制器 Angular 模块 导入      更新时间:2023-09-26

我试图从我的应用程序中导入的模块中使用控制器,但控制器回调函数从未调用。我在这里错过了一些东西,已经研究了几个小时,不能弄清楚我错在哪里?

给你一个方便的小提琴:https://jsfiddle.net/06vz8bu8/

<main ng-app="itemlist" class="itemlist">        
    <div ng-repeat="item in items">
      {{item.name}} : {{item.info}}
    </div>
</main>  

js

 var Module = angular.module('itemlist', [
   'itemlistControllers'
 ]);
 var CtrlModule = angular.module('itemlistControllers', []);
 CtrlModule.controller = ('listController', ['$scope'], function ($scope) {
   $scope.items = [
     {name: 'Foo', info: 'moo'}
   ];
 });

感谢您的帮助

这个怎么样(见代码注释):

 // .controller = ( <--- what was that????
 // In addition, explicit injections using this approach requires
 // both injected service/provider names and function in the same array
 CtrlModule.controller('listController', ['$scope', function ($scope) {
   $scope.items = [
     {name: 'Foo', info: 'moo'}
   ];
 }]);

在你的HTML中,你需要使用ng-controller指令显式地设置控制器,除非你使用UI Router或ngRoute:

<main ng-app="itemlist" class="itemlist" ng-controller="listController">