AngularJS:错误'控制器未定义'从另一个控制器调用抽象控制器时

时间:2014-05-13 06:45:21

标签: angularjs

在我的角度JS应用程序中,我有2个页面使用相同的功能,我正在尝试使用抽象控制器,我从“摘要”控制器中得到“Base Ctrl is not defined”错误。

我错过了什么......

加价

<div ng-controller="MainCtrl">
   <div ng-controller="SummaryCtrl">{{name}}</div>
   <div ng-controller="SearchCtrl"></div>
</div>

MaintCtrl.js

 define(['tabs/module'], function (module) {
   'use strict';
   module.controller('MainCtrl', ['$scope', function ($scope) {
    //some code;
   }]);
   function BaseCtrl($scope) {
     $scope.name="test";
   }
});

SummaryCtrl.js

 define(['tabs/summary/module'], function (module) {
   'use strict';
   module.controller('SummaryCtrl', ['$scope', function ($scope) {
    BaseCtrl($scope);
    //child actions`enter code here`
      $scope.name = 'Clicked from base controller';      
   }]);
});

1 个答案:

答案 0 :(得分:1)

我认为您喜欢使用此代码作为示例:

Angular: Extending controller

我仍然不能100%确定你的问题是什么,但在我看来,在你提供的这个样本中,两个代码都在同一个文件中。当您将它分成单独的文件时,请确保在引导或添加* .js文件引用时..确保在其他依赖的文件之前添加基数。在它上面

<强> EG

<script language="javascript" src="someUrl/BaseCtrl.js"></script>
<script language="javascript" src="someUrl/SummaryCtrl.js"></script>

修改

您的问题可能出在RequirejJS

看看

http://solutionoptimist.com/2013/09/30/requirejs-angularjs-dependency-injection/

您的MainModule已在&#39; tabs / module&#39;中注册,但您无处提供SummaryCtrl的任何依赖项。

EG。 SummaryCtrl不会隐式识别&#39;标签/模块&#39;,因为它位于标签/摘要/模块&#39;

尝试将此添加到摘要中:

 define(['tabs/summary/module', 'tabs/module'], function (module, BaseCtrl) {
   'use strict';
   module.controller('SummaryCtrl', ['$scope', function ($scope) {
    BaseCtrl($scope);
    //child actions`enter code here`
      $scope.name = 'Clicked from base controller';      
   }]);
});