學習angularjs時遇到 XX is not a function


當學習angularjs 遇到

Error: [ng:areq] Argument 'HelloController' is not a function, got undefined
http://errors.angularjs.org/1.3.15/ng/areq?p0=HelloController&p1=not%20a%20function%2C%20got%20undefined
minErr/<@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:63:12
assertArg@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:1587:1
assertArgFn@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:1597:1
$ControllerProvider/this.$get</<@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:8470:9
nodeLinkFn/<@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:7638:34
forEach@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:331:11
nodeLinkFn@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:7625:11
compositeLinkFn@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:7117:13
compositeLinkFn@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:7120:13
compositeLinkFn@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:7120:13
publicLinkFn@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:6996:30
bootstrapApply/<@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:1457:11
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:14466:16
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:14565:18
bootstrapApply@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:1455:9
invoke@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:4203:14
bootstrap/doBootstrap@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:1453:1
bootstrap@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:1473:1
angularInit@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:1367:5
@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:26304:5
trigger@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:2762:7
createEventHandler/eventHandler@http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js:3032:9

http://special.csc86.com/peng/AngularJS/js/angular-1.3.15/angular.js
Line 11655

如下圖:

出現這種問題的原因:

出現這個問題是因為在 angularJs 1.3 中 為了讓 根節點上(rootScope)不再被掛上許多冗余的內容,所以禁止了直接在根上注冊controller(即在全局作用域中創建控制器)。故下面這種寫法會報上面的錯誤:

解決方法:

第一:以后不能直接以  function XXXController ($scope){ code......}這樣的方式直接注冊監聽器了。以后必須

angular.module('hello', []).controller('HelloController',function($scope) {})這樣來將controller注冊到對應的模型上。

第二:  在 ng-aap 中指定相對應的模型。

如 之前:

<html ng-app>(不指定則為根元素)

現在:

<html ng-app="hello">

下面為修改並整理后不會報錯的代碼:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM