AngularJS - Directive not recognizing controller

user1177440

If I have a directive and a controller in the same file:

var app =  angular.module('app.navigation', []);

app.controller('NavItemCtrl', [ ....])

app.directive('navItem', [
        'ribbon', '$window', function (ribbon, $window) {
            return {
                require: ['^navigation', '^?navGroup'],
                restrict: 'AE',
                controller: 'NavItemCtrl',
      ...
      }])

Everything is fine, but if I move the controller code to a different file, because the current file is becoming too clutered, using the same module, and referencing the new controller file in my index page, the directive screams

          p0=NavItemCtrl&p1=not aNaNunction got undefined

My index page is like this:

 <html>
 <body>
  ....

 <script app.js ...>
 <script new controller file path .... >
 <script original directive file path .... >
  ....
 </body>
 </html>

What am I missing here?

[Solution] Delta is right, I figured it out:

For good housing keeping, I think it may be wise to have one JS file, listed as a dependency in the main app.js, that instantiates all the modules you will use, assuming your project is becoming large, and then reuse that instantiate w/o having any dependencies.

As example:

(1) Main App:

angular.module('MainApp', ['ngRoute', 'ngAnimate', 'app.SubApp1', 'app.SubApp2', 'app.SubApp3' ...]

(2) Then as a repository, if you will, create a new js file, say repositoryApp.js, instantiating these sub apps, making sure that this file is referenced before all other files that will use these sub app modules:

angular.module('app.SubApp1', [xxx]);
angular.module('app.SubApp2', [xxx]);
angular.module('app.SubApp3', [xxx]);

(3) Then when creating a series of directives, controllers, or whatever pertaining to a particular sub app, merely reference that sub app in the respective file as:

angular.module('app.SubApp1').controller('foo') .....
angular.module('app.SubApp1').directive('bar') .....

Without the dependency brackets as that is what threw the error for me.

Delta

in your directive are you getting you app like this

var app =  angular.module('app.navigation');

if you put the brackets after it like your first example you will just be replacing what you have currently instead of getting it.

This get a new module

var app =  angular.module('app.navigation', []);

This gets an existing modules.

var app =  angular.module('app.navigation');

Notice the exclusion of the brackets in the second example.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

angularjs中Controller和Directive之间的通信

来自分类Dev

AngularJS将更改的值从Controller传递到Directive Controller

来自分类Dev

AngularJS将更改的值从Controller传递到Directive Controller

来自分类Dev

AngularJS & Typescript: How to assign a class/type as a directive's controller?

来自分类Dev

AngularJS overwrites isolated directive scope

来自分类Dev

AngularJS Directive for downloading excel file

来自分类Dev

模板中的AngularJS“ Controller as”

来自分类Dev

AngularJS controller return value

来自分类Dev

AngularJS controller and service creation

来自分类Dev

if statement in ng-repeat directive in AngularJS

来自分类Dev

AngularJS - Injecting Factory into Directive's link function

来自分类Dev

Change the templateUrl of directive based on screen resolution AngularJS

来自分类Dev

AngularJS ngRepeat directive does not have compile option

来自分类Dev

How to separate Controller file in angularjs

来自分类Dev

AngularJS:“ Controller as”语法和指令

来自分类Dev

AngularJS'controller as'和form。$ valid

来自分类Dev

在Controller AngularJS中创建函数

来自分类Dev

Controller defined in a module referenced inside a directive in another module

来自分类Dev

angularjs: force re-rendering/ full refresh a directive template

来自分类Dev

AngularJS Directive包装器使用Kendo泄漏内存

来自分类Dev

AngularJS-将Factory注入Directive的链接函数

来自分类Dev

Unit test AngularJS directive with template containing ng-if

来自分类Dev

AngularJS - issue when embedding the Youtube video url with directive

来自分类Dev

Using $compile in a directive triggers AngularJS infinite digest error

来自分类Dev

AngularJS: manual $compile vs natural $compile over a recursive directive

来自分类Dev

AngularJS module.directive不一致响应

来自分类Dev

AngularJS Leaflet Directive $ scope.markers在拖动时不会更新

来自分类Dev

AngularJS module.directive不一致响应

来自分类Dev

AngularJS-将Factory注入到Directive的链接函数中

Related 相关文章

  1. 1

    angularjs中Controller和Directive之间的通信

  2. 2

    AngularJS将更改的值从Controller传递到Directive Controller

  3. 3

    AngularJS将更改的值从Controller传递到Directive Controller

  4. 4

    AngularJS & Typescript: How to assign a class/type as a directive's controller?

  5. 5

    AngularJS overwrites isolated directive scope

  6. 6

    AngularJS Directive for downloading excel file

  7. 7

    模板中的AngularJS“ Controller as”

  8. 8

    AngularJS controller return value

  9. 9

    AngularJS controller and service creation

  10. 10

    if statement in ng-repeat directive in AngularJS

  11. 11

    AngularJS - Injecting Factory into Directive's link function

  12. 12

    Change the templateUrl of directive based on screen resolution AngularJS

  13. 13

    AngularJS ngRepeat directive does not have compile option

  14. 14

    How to separate Controller file in angularjs

  15. 15

    AngularJS:“ Controller as”语法和指令

  16. 16

    AngularJS'controller as'和form。$ valid

  17. 17

    在Controller AngularJS中创建函数

  18. 18

    Controller defined in a module referenced inside a directive in another module

  19. 19

    angularjs: force re-rendering/ full refresh a directive template

  20. 20

    AngularJS Directive包装器使用Kendo泄漏内存

  21. 21

    AngularJS-将Factory注入Directive的链接函数

  22. 22

    Unit test AngularJS directive with template containing ng-if

  23. 23

    AngularJS - issue when embedding the Youtube video url with directive

  24. 24

    Using $compile in a directive triggers AngularJS infinite digest error

  25. 25

    AngularJS: manual $compile vs natural $compile over a recursive directive

  26. 26

    AngularJS module.directive不一致响应

  27. 27

    AngularJS Leaflet Directive $ scope.markers在拖动时不会更新

  28. 28

    AngularJS module.directive不一致响应

  29. 29

    AngularJS-将Factory注入到Directive的链接函数中

热门标签

归档