【问题标题】:div in ng-repeat is not getting updated?ng-repeat 中的 div 没有更新?
【发布时间】: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 没有更新?

【问题讨论】:

  • 你的代码看起来不错,plnkr.co/edit/LfnTyxX6Iw3GLcPxRNbf?p=preview
  • Here 是 $http 调用的工作示例
  • @JaganathanBantheswaran 这不起作用。你能更新一下吗?
  • 我在 FF、Chrome 中测试了链接。您使用的是哪种浏览器?

标签: javascript html angularjs get auto-update


【解决方案1】:

请通过在末尾添加“)”来关闭您的 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 代码并检查。一旦它被修复,让我知道。快乐编码:)

【讨论】:

  • 嗨,我在这个页面中看到了一些 plnkr 示例。这就是你要找的场景。因为我在示例中更新了 div。
  • 能把看过例子的网址放上去
  • 请将您的 js 文件替换为我在上面发布的代码。快乐的编码:)
【解决方案2】:

首先,我认为您不需要 $scope.$apply() 来触发摘要循环。您处于角度上下文中。

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

第三,如果该属性是对象中的最后一个属性,则您的响应对象不应在用户名属性后有逗号。

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

【讨论】:

  • 我已经更新了问题。请看上面并帮助我解决这个问题
  • 在 http 调用中仍然缺少 ')'。 $http.get("test1.json") .then(function(response) { $scope.profiles = response.data.profiles; }; 应该是 $http.get("test1.json ") .then(function(response) { $scope.profiles = response.data.profiles; });
  • 我已经改变了。但这不是问题。
猜你喜欢
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2017-03-08
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多