我正在嘗試使用AngularJS,而我的「ng-controller」不起作用。angularjs ng-controller不起作用
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="lib/angular.js"></script>
</head>
<body ng-app>
<h1>Add user</h1>
<form action="addUser" method="post">
<input type="text" name="pseudo" placeholder="pseudo">
<input type="password" name="password" placeholder="password">
<input type="submit" value="envoyer">
</form>
{{1 + 2}}
<div ng-controller="myController">
{{ name }}
</div>
<a href="./remove">supression</a>
<script type="text/javascript">
function myController($scope){
alert("test");
$scope.name = 'toto';
}
</script>
</body>
</html>
當我想這在Chrome,{{1+2}}
正確的 '3' 所取代,但{{name}}
不是。我預計'toto'。
我也想上顯示點擊事件的警報,這是行不通的:
<script type="text/javascript">
var myApp = angular.module('MyApp', []);
myApp.controller('myController', ["$scope",function($scope){
$scope.name = 'toto';
$scope.onMyButtonClick = function(){
alert("test");
}
}])
</script>
HTML
<div ng-controller="myController">
{{ name }}
<button ng-click="onMyButtonClick">test</button>
</div>
當我點擊,什麼也不會發生。
角度的功能,必須創建渲染體前。在你的頭標上添加腳本 –
這也行不通:/ – Raph