angular.js - angular 的controller 不识别function

浏览:23日期:2023-02-07

问题描述

代码:

<!doctype html><html><head> <link rel='stylesheet' href='https://www.6hehe.com/node_modules/bootstrap/dist/css/bootstrap.css'> <script src='https://www.6hehe.com/node_modules/angular/angular.js'></script></head><body> <p ng-app ng-controller='HelloCtrl'>Your name: <input type='text' ng-model='name' placeholder='World'><hr> Hello {{name}}! </p><script> var HelloCtrl = function($scope){ $scope.name = ’world’; }</script></body></html>

一个angular的例子,但是总是报错:

Error: [ng:areq] Argument ’HelloCtrl’ is not a function, got undefined http://errors.angularjs.org/1.3.15/ng/areq?p0=HelloCtrl&p1=not%20a%20function%2C%20got%20undefined at REGEX_STRING_REGEXP (angular.js:63) at assertArg (angular.js:1587) at assertArgFn (angular.js:1597) at angular.js:8470 at angular.js:7638 at forEach (angular.js:331) at nodeLinkFn (angular.js:7625) at compositeLinkFn (angular.js:7117) at publicLinkFn (angular.js:6996) at angular.js:1457

不知道是为何~~?

后来,我试了网上的例子:

<script type='text/javascript'> var app = angular.Module(’app’,[]); app.controller(’HelloCtrl’,function($scope){$scope.name = ’world’; });</script>

修改了这部分,但是还是报错:

Uncaught TypeError: undefined is not a functionangular.js:63 Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:Error: [$injector:nomod] Module ’app’ is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

希望谁能帮忙给指正下,给个正确的写法。

问题解答

回答1:

第一个:不知道你的angular的版本,大概angular从1.3(具体版本不记得了)之后就不再支持全局controller的写法了。现在网上的很多示例已经不适用了。第二个:注意报错的地方很明显,就是angular.Module,这里你写的是大写,应该是小写angular.module。还有就是,利用这种方式的时候,需要给模板上加上ng-app='app',指定模块名。

回答2:

楼上正解,1.3之后不支持全局控制器了

相关文章: