当ng-repeat更新时,HTML表没有更新
html table not getting updated when ng-repeat is updated
我尝试通过添加值的形式来更新表,范围变量得到更新,但表仍然未更新。我的表
<tbody>
<tr ng-repeat="ct in city">
<td>
<center>[ {{ct.location.latitude}} , {{ct.location.longitude}} ]</center>
</td>
<td>
{{ct.name}}
</td>
my function to update list
window.siteadd = function(){
var citlat = document.getElementById("citylat").value;
var citlan = document.getElementById("citylon").value;
var citname = document.getElementById("cityname").value;
var citstat = document.getElementById("citynostation").value;
var citytemp = {
location:{
latitude: citlat,
longitude: citlan
},
name: citname,
}
console.log("temp", citytemp);
$scope.city.push(citytemp);
console.log("tempadded", $scope.city);
}
表没有更新,因为函数siteadd
没有在angular的上下文中运行,因此$digest
循环没有由它运行以更新视图中的所有绑定。
为了使它成为可能,你必须在你的函数中显式地调用$digest
循环,或者只是将函数包装在$scope.$apply
中。
对于你的代码:
window.siteadd = function(){
var citlat = document.getElementById("citylat").value;
var citlan = document.getElementById("citylon").value;
var citname = document.getElementById("cityname").value;
var citstat = document.getElementById("citynostation").value;
var citytemp = {
location:{
latitude: citlat,
longitude: citlan
},
name: citname,
}
console.log("temp", citytemp);
$scope.city.push(citytemp);
console.log("tempadded", $scope.city);
$scope.$digest(); //just add this line and it will work.
}
/*****供您参考*****/
为了更好地理解$watch, $digest and $apply
,请参考链接"http://angular-tips.com/blog/2013/08/watch-how-the-apply-runs-a-digest/"
var app=angular.module("app",[])
app.controller("itemController",function($scope){
$scope.citytemp = {
location:{
latitude: null,
longitude: null
},
name: null
}
$scope.city=[];
$scope.city.push($scope.citytemp);
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script>
</head>
<body ng-app="app">
<div ng-controller="itemController">
City Name: <input type='text' ng-model='citytemp.name'><br>
Latitude: <input type='text' ng-model='citytemp.location.latitude'><br>
Longitude: <input type='text' ng-model='citytemp.location.longitude'><br>
<table>
<tbody>
<tr ng-repeat="ct in city">
<td>
City Name: {{ct.name}}
<br> Location : [ {{ct.location.latitude}} ,{{ct.location.longitude}} ]
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
相关文章:
- 使用 node.js 和 javascript 更新 HTML 对象
- 如何从弹簧控制器方法更新html img src
- 以非阻塞方式更新HTML画布
- 如何在加载页面后更新 HTML
- 根据json更新html
- Angular数组更改时更新HTML视图
- 根据ajax json响应更新html表
- 从数组中删除值,用新的数组总数更新html或'$'如果没有总计
- 测试React中的点击事件是否会更新HTML
- 动态更新Html中的列表
- Jquery函数更新html
- JavaScript/jQuery:在选择列表更改时更新HTML
- 通过ajax更新HTML块内容
- 当我更新HTML时,Keydown事件停止触发
- 动态更改语言时,应更新HTML lang属性
- 更新html页面中的数据
- 如何使用BackboneJs获取JSON数据和更新html
- 如何在更改一列时更新HTML表行中的单元格
- 无法使用 innerHTML 使用 JavaScript 更新 HTML 表
- 当表单使用 ajax 和 Codeigniter 更改时自动更新 html