Code:
<!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>
An angular example, but an error is always reported:
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
I don’t know why~~?
Later, I tried the example online:
<script type="text/javascript">
var app = angular.Module('app',[]);
app.controller('HelloCtrl',function($scope){
$scope.name = 'world';
});
</script>
I modified this part, but still got an error:
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.
I hope someone can help me to correct it and give me the correct way to write it.
First one:
I don’t know your version of Angular. Probably Angular no longer supports the writing of global controller since version 1.3 (I can’t remember the specific version). Many of the examples online are no longer applicable.
Second one:
Note that the error reported is obvious, which is
angular.Module
,这里你写的是大写,应该是小写angular.module
。还有就是,利用这种方式的时候,需要给模板上加上ng-app="app"
, specifying the module name.Correct answer upstairs, global controllers are no longer supported after 1.3