代碼:
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.css">
<script src="../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 function
angular.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.
希望誰能幫忙給指正下,給個正確的寫法。
我想大声告诉你2017-05-15 16:53:04
第一個:
我不知道你的angular的版本,大概angular從1.3(具體版本不記得了)之後就不再支援全域controller的寫法了。現在網路上的許多範例已經不適用了。
第二個:
注意報錯的地方很明顯,就是angular.Module
,这里你写的是大写,应该是小写angular.module
。还有就是,利用这种方式的时候,需要给模板上加上ng-app="app"
,指定模組名。