【发布时间】:2016-10-18 10:50:30
【问题描述】:
我收到来自 http get 请求的响应,我正在使用响应来迭代数据
<div id="container" ng-app="myApp" ng-controller="myctrl">
<div ng-repeat="profile in profiles">
<p>{{profile.username}}</p>
</div>
</div>
这是为了在点击时添加一个配置文件
<input ng-click="addProfile()" type="button" value="add Profile" />
这是我在控制器中的获取请求
var app = angular.module('myApp', []);
app.controller('myctrl', function($scope, $http) {
$scope.profiles = [];
$http.get("test1.json")
.then(function(response) {
$scope.profiles = response.data.profiles; //Response is provided below
});
$scope.addProfile = function(){
$http.get('test2.json')
.then(function(response) {
alert("af");
$scope.items = response.data.profiles; //Response is provided below
$scope.profiles.push($scope.items);
console.log($scope.profiles); //gives the update Array
});
});
});
我的 test1.json 获取请求的响应
{
"Id": "44442232",
"profiles": [
{
"type": "Friend",
"username": "Username 1",
},
{
"type": "Student ",
"username": "Username 2",
}
]
}
我的 test2.json 获取请求的响应
{
"Id": "44442232",
"profiles": [
{
"type": "Friend",
"username": "Username 4",
},
{
"type": "Student ",
"username": "Username 5"
}
]
}
我在数组中更新后尝试了console.log。数组已更新但ng-repeat 中的div 没有更新?
【问题讨论】:
-
-
Here 是 $http 调用的工作示例
-
@JaganathanBantheswaran 这不起作用。你能更新一下吗?
-
我在 FF、Chrome 中测试了链接。您使用的是哪种浏览器?
标签: javascript html angularjs get auto-update