[英]div in ng-repeat is not getting updated?
I am getting a response from http get request and Iam using the response to iterate in the data 我从http get请求中获得响应,而Iam使用响应来迭代数据
<div id="container" ng-app="myApp" ng-controller="myctrl">
<div ng-repeat="profile in profiles">
<p>{{profile.username}}</p>
</div>
</div>
This is for adding one profile on click 这是用于在点击时添加一个配置文件
<input ng-click="addProfile()" type="button" value="add Profile" />
Here is my get request in the controller 这是我在控制器中的获取请求
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
});
});
});
response of my get request of test1.json 我对test1.json的获取请求的响应
{
"Id": "44442232",
"profiles": [
{
"type": "Friend",
"username": "Username 1",
},
{
"type": "Student ",
"username": "Username 2",
}
]
}
response of my get request of test2.json 我对test2.json的获取请求的响应
{
"Id": "44442232",
"profiles": [
{
"type": "Friend",
"username": "Username 4",
},
{
"type": "Student ",
"username": "Username 5"
}
]
}
I have tried console.log
after updating in the array.The array is updated but the div in ng-repeat
is not getting updated? 我在数组中更新后尝试了console.log
已更新但ng-repeat
的div没有更新?
Please close your JSON calling request by putting ')' at the end. 请在末尾加上“)”,以关闭JSON调用请求。 There is no error apart from this and the same code works for me fine. 除此之外没有任何错误,相同的代码对我来说很好。 // Code goes here //代码在这里
var myApp = angular.module("myApp", []);
myApp.controller("myctrl", function($scope, $http) {
$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) {
$scope.items = response.data.profiles; //Response is provided below
angular.forEach($scope.items, function(value) {
$scope.profiles.push(value);
});
console.log($scope.profiles);
});
};
}); });
Please find this updated js code and check. 请找到此更新的js代码并检查。 Once it is fixed let me know. 一旦修复,请通知我。 happy coding :) 快乐的编码:)
First of all I don't think you need $scope.$apply() to trigger the digest cycle. 首先,我认为您不需要$ scope。$ apply()来触发摘要循环。 You are in angular context. 您处在有角度的环境中。
Second, you are missing a ')' when you are calling .then() . 其次,您在调用.then()时缺少一个')'。 You currently have '.then(function(){};' 您目前拥有'.then(function(){};'
Third, your response object should not have a comma after the username property if that property is the last one in the object. 第三,如果响应属性是对象中的最后一个,则该响应对象的username名称后面不应包含逗号。
Also do you have a silent fail, or it is an error in the console, and the processed html for the ng-repeat is blank or you get the angular interpolation syntax {{profile.username}}? 另外,您是否有无提示的失败,或者是控制台中的错误,并且ng-repeat的已处理html为空白,还是获得了角插值语法{{profile.username}}?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.