繁体   English   中英

ng-repeat中的div无法更新?

[英]div in ng-repeat is not getting updated?

我从http get请求中获得响应,而Iam使用响应来迭代数据

<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没有更新?

请在末尾加上“)”,以关闭JSON调用请求。 除此之外没有任何错误,相同的代码对我来说很好。 //代码在这里

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);

     });

 };

});

请找到此更新的js代码并检查。 一旦修复,请通知我。 快乐的编码:)

首先,我认为您不需要$ scope。$ apply()来触发摘要循环。 您处在有角度的环境中。

其次,您在调用.then()时缺少一个')'。 您目前拥有'.then(function(){};'

第三,如果响应属性是对象中的最后一个,则该响应对象的username名称后面不应包含逗号。

另外,您是否有无提示的失败,或者是控制台中的错误,并且ng-repeat的已处理html为空白,还是获得了角插值语法{{profile.username}}?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM