[英]Why this simple Angular.js example does not work?
我正在阅读O'REILLY的 AngularJS ,尝试通过示例查看angular的工作原理,但可以使它起作用:
hello.html :
<html ng-app>
<head>
<script src="angular.js"></script>
<script src="controllers.js"></script>
</head>
<body>
<div ng-controller="HelloController">
<p>{{ greeting.text }}, World</p>
</div>
</body>
</html>
以及controllers.js中的逻辑:
function HelloController($scope) {
$scope.greeting = { text: 'Hello' };
}
但是当我在浏览器上显示hello.html时,我可以看到{{ greeting.text }}, Hello
。
怎么了
您从未定义过控制器,而只是定义了名称中恰好具有“ controller”的函数。
尝试正确初始化应用程序:
var myApp = angular.module('myApp',[]);
myApp.controller('HelloController', ['$scope', function($scope) {
$scope.greeting = {text: 'Hello'};
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.