HelloWorld-Argument 'HelloController' is not a function, got undefined

学习angular,按照《用AngularJS开发下一代web应用》中的入门HelloWorld示例,结果搞半天都不出来

网上查了下坑爹的issue,只能说技术更新太快,老祖先的话绝没错:逆水行舟,不进则退。

转:

没错,这就是开头所说的那个。于是我弄不懂,为什么老师可以跑通,我却跑不通,大家都是Angular-1.3.0。

国内不给力,很难找到答案,最后去StackOverflow找到了。因为从Angular-1.3.0 beta15开始,Angular不再支持全局的Controller!

那么问题来了,如何在Angular里定义模块呢?

   当然这篇blog是为了让大家学习Angular的时候别遇到闭门羹,我大致说一下。AngularJs里,有一个angular.module,模块机制就是靠这个来加载的。

controller.js

var myAppModule = angular.module("myApp",[]);

myAppModule.controller(‘HelloController‘,
    function($scope) {
        $scope.greeting = {text:‘Hello‘};
    }
);

hello.html

<html ng-app="myApp">
    <head>
        <script src="../angular.min.js"></script>
        <script src="controller.js"></script>
    </head>
    <body>
        <div ng-controller=‘HelloController‘>
            <p>{{greeting.text}},World</p>
        </div>

    </body>
</html>

    我们看看,改了什么,ng-app="myApp"首先给入口赋了名字,然后再下面进行模块的初始化

    在angular.module(String, moduleArray)函数里

第一个String参数是该模块的名字第二个参数是所依赖的模块(这里暂时用不着),

返回了myApp这个模块,然后再通过模块的controller方法定义HelloController控制器即可

第二个参数依赖模块小示例

var myAppModule = angular.module("myApp",["basedHelloController"]);

var controllerModule = angular.module("basedHelloController",[]);

controllerModule.controller(‘HelloController‘,
    function($scope) {
        $scope.greeting = {text:‘HelloBasedModule‘};
    }
);

仅仅是多定义一个basedHelloController模块,这样myApp,就可以仅仅作为启动口,而不会混杂着控制器。我们可以定义多个js文件,清晰明确的通过模块梳理整个项目的结构。

引用 http://www.cnblogs.com/YikaJ/p/4226313.html

HelloWorld-Argument 'HelloController' is not a function, got undefined

时间: 2024-07-31 09:34:57

HelloWorld-Argument 'HelloController' is not a function, got undefined的相关文章

Argument &#39;xxx&#39; is not a function, got undefined,初学Angular的第一个坑

终于考完试了,在没更新的这一段时间里,一直都在忙于应付考试.不过在期间也是接触到不少好玩的东西,比如Html5的Canvas,我用lufylegend的Html5引擎做了个<看你有所色>的游戏.还有最近刚开始玩的Angular. Angular也是早有听说了啊,一直没闲下功夫研究,趁着放假,学一学.慕课网(www.imooc.com)里有一套教程,还是很不错的.但是真正上手编码的时候就发现,尼玛!例子都跑不起来,全是报错,Argument 'xxx' is not a function, go

Error: [ng:areq] Argument &#39;LoginCtrl&#39; is not a function, got undefined

"LoginCtrl'"该控制器也定义了 改为以下 最后发现: Error: [ng:areq] Argument 'LoginCtrl' is not a function, got undefined

【AngularJs】---&quot;Error: [ng:areq] Argument &#39;fn&#39; is not a function, got Object&quot;

项目中把controller.service抽取出来 一步一步没有报错 index那里加 <script src="js/controllers/XXController.js"></script>就报错了 [原因] 我抽取出来的controller头部也这样写了 angular.module('gflt.controllers', []) 正确写法 angular.module('gflt.controllers') [AngularJs]---"E

对IIFE中(function(window,undefined){})(window)中为什么传递undefined的理解

1.在函数中定义形参但没有传递实参就相当于定义了变量但未赋值,所以下面中的a就是undefined 1 function test(a){ 2 console.log(a)//undefined 3 }; 4 test(); 2.在IIFE中,window作为实参传递进去,避免了执行代码时,每次都到全局中寻找window从而提高了效率,但是为什么形参中还要指定一个undefined呢,上面说过在函数中定义了形参就相当于定义了但不赋值,那在这个立即执行函数中undefinde还是undefined

JS 关于(function( window, undefined ) {})(window)写法的理解【转】

JS 关于(function( window, undefined ) {})(window)写法的理解 [网络整理] (function( window, undefined ) {})(window); 这个,为什么要将window和undefined作为参数传给它? (function( $, undefined ) {})(jQuery); 同理 因为 ecmascript 执行JS代码是从里到外,因此把全局变量window或jQuery对象传进来,就避免了到外层去寻找,提高效率.und

;(function( $, window, undefined ){ }(jQuery,window))为何需要往里面传$,window,undefined这些参数

(function( $, jQuery , undefined ) {})(jQuery); 为什么要将window和undefined作为参数传给它? 因为 ecmascript 执行JS代码是从里到外,因此把全局变量window或jQuery对象传进来,就避免了到外层去寻找,提高效率.undefined在老一辈的浏览器是不被支持的,直接使用会报错,js框架要考虑到兼容性,因此增加一个形参undefined. var undefined = 8; (function( window ) {

jquery源码中的(function(window, undefined){})(window)【转】

(function( window, undefined ) {})(window);这个,为什么要将window和undefined作为参数传给它? (function( $, undefined ) {})(jQuery); 同理 因为 ecmascript 执行JS代码是从里到外,因此把全局变量window或jQuery对象传进来,就避免了到外层去寻找,提高效率.undefined在老一辈的浏览器是不被支持的,直接使用会报错,js框架要考虑到兼容性,因此增加一个形参undefined. 还

JS 关于(function( window, undefined ) {})(window)写法的理解

JS 关于(function( window, undefined ) {})(window)写法的理解 [网络整理] (function( window, undefined ) {})(window);这个,为什么要将window和undefined作为参数传给它? (function( $, undefined ) {})(jQuery); 同理 因为 ecmascript 执行JS代码是从里到外,因此把全局变量window或jQuery对象传进来,就避免了到外层去寻找,提高效率.unde

关于jQuery源码中(function(window,undefined){//dosomething()})(window)写法解释

一.首先是最常见的闭包 (Closure) 范式自执行函数的写法,这里用匿名函数封装(构造块级作用域),避免了匿名函数内部的代码与外部之间发生冲突(如使用了相同的变量名). 1 (function() {// ...})(); 二.自执行函数和其他函数类似,都可以传入参数:jQuery源码中将window作为一个参数传入, window是DOM对象模型的最顶层对象,把全局变量传进来,就避免了到外层去寻找,提高效率: 1 (function(window) {// ...})(window); 当