類這裏我有一個片斷(下文)。 它的作用是,當你的身體單擊第一個項目(的「Hello World」)是由綠色,當我點擊按鈕‘clicky’我取出的第一個項目,在註釋彈出一個元素,angularJS在觀看循環後重新呈現HTML,當它保持第一個元素的相同綠色樣式時。我希望能夠從ng-repeat樹中的模板中重新繪製所有的元素。這將返回所有「Hello World」爲紅色而不是綠色。 我是否缺少一些非常基本的東西?角JS不更新或重新繪製
<html>
<head>
<title>
Kaboom
</title>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js" />
<script>
</script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.annotations = [
{id: -1, name:"Hello World"},
{id: -2, name:"Hello World"},
{id: -3, name:"Hello World"}
];
$scope.remove = function() {
$scope.annotations.splice(0,1);
console.log($scope.annotations);
}
});
</script>
<script>
function change() {
var children = document.getElementById("test").children;
console.log(children);
children[0].setAttribute("class", "green");
}
</script>
<style>
.red {
color: red;
}
.green {
color: green;
}
</style>
</head>
<body onclick = "change()" ng-app="myApp" ng-controller="myCtrl">
<div id = "test" ng-repeat="t in annotations track by $index">
<div class = "red" id = "{{t.id}}" >{{t.name}}</div>
</div>
<input type="button" value="clicky" ng-click = "remove()" />
</body>
</html>