[英]Adding ng-controller stops AngularJS
調用下面的 HTML AngularJS 並且網頁打印“5”作為“body”中 {{10/2}} 的結果。
<!DOCTYPE html>
<html ng-app>
<head>
<title>AngularJS</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<h2>hello angular</h2>
{{ 10/2 }}
</body>
</html>
當我添加一個 ng-controller 時:
<!DOCTYPE html>
<html ng-app>
<head>
<title>AngularJS</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-controller="MainController">
<h2>hello angular</h2>
{{ 10/2 }}
</body>
</html>
使用 script.js:
var MainController = function($scope){
$scope.message = "Hello!"
}
AngularJS 停止工作,網頁打印 {{10/2}} 而不是 5。我在這里做錯了什么? 謝謝。
在script.js
,更改
var MainController = function($scope){
$scope.message = "Hello!"
}
到
app.controller('MainController', function($scope) {
$scope.message = "Hello!"
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.