w3c school上有如下代码。报错:[ng:areq] http://errors.angularjs.org/1.3.15/ng/areq?p0=personController&p1=not%20a%20function%2C%20。
<!DOCTYPE html>
<html>
<body>
<div ng-app="" ng-controller="personController">
名: <input type="text" ng-model="person.firstName"><br>
姓: <input type="text" ng-model="person.lastName"><br>
<br>
姓名: {{person.firstName + " " + person.lastName}}
</div>
<script>
function personController($scope) {
$scope.person = {
firstName: "John",
lastName: "Doe"
};
}
</script>
<script src="//www.runoob.com/try/angularjs/1.2.5/angular.min.js"></script>
</body>
</html>
纠其原因:应该是一个控制器必须定义在一个模块中,并且模块的名字不得为空。
修改后为:
<!DOCTYPE html>
<html>
<body>
<div ng-app = "rose" ng-controller="personController">
名: <input type="text" ng-model="person.firstName"><br>
姓: <input type="text" ng-model="person.lastName"><br>
<br>
姓名: {{person.firstName + " " + person.lastName}}
</div>
<script src="D:\automation\tools\data_desensitization\src\main\webapp\webapps\app\bower_components\angular\angular.min.js">
</script>
<script>
angular.module('rose',[]).controller('personController',function personController($scope) {
$scope.person = {
firstName: "John",
lastName: "Doe"
};
});
</script>
</body>
</html> 运行正确