本文介绍了如何找到一个理由AngularJS"参数“MyCtrl”不是一个函数,得到了不确定"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个错误AngularJS崩溃参数MyCtrl不是一个函数,得到了不确定这可能是有点挑战性找到一个原因。

下面我想打一个样的检查表中,你该怎么检查时,得到一个错误


解决方案

  1. 是与'MyCtrl通过HTML连接文件? (如果你Concat的两次检查或丑化您的文件)

<脚本的src ='路径/要/ controllers.js'>< / SCRIPT>


  • 为MyCtrl'正确定义

  • 有一些图案:

    app.controller('MyCtrl',['$范围',函数($范围内){...}] )app.controller('MyCtrl',函数($范围内){...})VAR MyCtrl =功能($范围内){...})


  • 是'MyCtrl的权利模块中定义的?

  • 是MyCtrl的模块添加到应用程序的依赖性

  • angular.module('应用',['app.sources']);


  • 如果你定义的模块的多个次,你应该在这个顺序定义它


    • 首先定义应该是这样


  • angular.module('app.sources',[]);

    (是 []


    • 随后定义应该是这样

    angular.module('app.sources');

    (不含 []

    重要提示:声明顺序是非常重要的 - 与定义的 [] 应先走


  • 检查模块只定义一次。
    你可能已经忘记了复制粘贴后重命名模块。检查SRC像串

  • angular.module('app.sources',[]);


  • 检查 'NG-应用。最好只使用其中的一个与名称,如 NG-应用='应用'(换句话说不定义多个无名ngApp指令)


  • 是您的控制器的语法的为您正确AngularJS版本


  • (有角中的 1.0.x的 1.2.x版本及更高版本。具有角版本比 1.3.x的更大,你不能声明一个全局的构造函数和NG-控制器使用)

    When AngularJS crashes with an error "Argument 'MyCtrl' is not a function, got undefined" it's may be little bit challenging to find a reason why.

    Here I want to make a kind of a "check list", what should you check when got an error

    解决方案
    1. Is file with 'MyCtrl' connected via html? (check twice if you concat or uglify your files)
    <script src='path/to/controllers.js'></script>
    
    1. Is 'MyCtrl' defined correctly?

    There are a few patterns:

    app.controller('MyCtrl', ['$scope', function ($scope) {...}])
    
    app.controller('MyCtrl', function ($scope) {...})
    
    var MyCtrl = function ($scope) {...})
    
    1. Is 'MyCtrl' defined in right module?
    2. Is "MyCtrl's" module added to app dependencies?
    angular.module('app', ['app.sources']);
    
    1. If you define your module multiple times, you should define it in this order:

      • First define should be like
    angular.module('app.sources', []);
    

    (with [ ])

    • Subsequent defines should be like

    angular.module('app.sources');
    

    (without [ ])

    Important: Declaration order is important - definition with [ ] should go first.

    1. Check that module is defined only once.You may have forgotten to rename module after copy-paste. Check src for string like
    angular.module('app.sources', []);
    
    1. Check your 'ng-app'. Better to use only one of these with name like ng-app='app' (In other words do not define multiple unnamed ngApp directives)

    2. Is your controller's syntax correct for your AngularJS version?

    (There is a difference between definition in Angular 1.0.x and 1.2.x and higher. With Angular versions greater than 1.3.x, you cannot declare a global constructor function and use it with ng-controller)

    这篇关于如何找到一个理由AngularJS&QUOT;参数“MyCtrl”不是一个函数,得到了不确定&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-24 13:29