【发布时间】:2017-04-07 06:49:02
【问题描述】:
这里我有一个 sn-p(下)。 它的作用是,当您单击主体时,第一项(“Hello World”)变为绿色,当我单击按钮“clicky”时,我正在删除第一项,通过在注释中弹出一个元素,然后 angularJS 重新在监视周期之后呈现 HTML,当它完成时,它为第一个元素保留相同的绿色样式。我期待 Angular 从 ng-repeat 树中存在的模板从头开始重新绘制所有元素。这将返回所有“Hello World”为红色而不是绿色。 我错过了一些非常基本的东西吗?
<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>
【问题讨论】:
标签: javascript html angularjs