【问题标题】:How do you update an existing item in an Angular Array (that has changed externally)?如何更新 Angular Array 中的现有项目(已在外部更改)?
【发布时间】:2014-07-15 21:41:06
【问题描述】:

我是 Angular 的新手,正在努力更新我的 Angular 数组中已在外部更改的现有项目(不是通过 Angular 驱动的 UI)。

这是用例...我的网页是通过服务器端调用填充的,我正在将数组加载到 Angular 中并显示在列表中。

现在,如果服务器上的数据发生变化并且在表中插入了新记录,我的页面的 JavaScript 会收到通知,并通过“推送”成功地将新记录插入到 Angular 数组中(参考Programmatically inserting array values in Angular JS)。

但是,当现有记录发生更改时(在服务器端/不是通过 Angular 驱动的 UI),我的页面也会收到通知。我对如何更新 Angular 数组中的正确记录一无所知? Angular 文档中是否有我遗漏的查询/更新方法?

这是我当前的代码的样子...

    //The HTML UI updates to reflect the new data inserts.
    <div ng-repeat="item in items">        
        <p class="priority">{{item.priority_summary}}</p>
        <p class="type">{{item.type}}</p>
    </div>

这是脚本...

    var app = angular.module('DemoApp', []);

    <!-- Define controller -->
    var contrl = app.controller("MainController", function ($scope) {

    $scope.items = [{
        "status": "New",
            "priority_summary": "High"
    }, {
        "status": "New",
            "priority_summary": "High"
    }, {
        "status": "New",
            "priority_summary": "High"
    }, {
        "status": "New",
            "priority_summary": "High"
    }];

//The insert works fine. The question is how do I do an update if the notification is for an update, and not for insert.

    $scope.addItem = function(item)
    {
     alert("addItem called");
     $scope.items.push(item);
     $scope.item = {};
    }

      $scope.subscribe = function(){
        //Code for connecting to the endpoint...
        alert("event received"); //We receive this alert, so event is received correctly.

        $scope.items.push({
          status: 'New',
          priority_summary: 'H'
        });
        $scope.$apply();

        }

      //calling subscribe on controller initialization...
      $scope.subscribe();

任何强调这一点的建议或示例都会很棒。谢谢!

【问题讨论】:

  • 您在寻找什么?您已经收到更改通知,而且您可以推送更新的数据?
  • @hasH:我能够插入新项目,但我不确定如何更新数组中的现有项目。典型的 Angular 过滤器似乎面向 UI,而我正在尝试对数组的项目本身进行更改。这有意义吗?
  • 能否获取需要更新的数据索引?
  • 是的,我应该提到这一点。我收到的 JSON 中的每条记录都有一个唯一的 ID。我是像往常一样在数组中搜索,还是有一种 Angular 方法让它变得简单?
  • 我认为您需要将更新的数据替换为数组中的正确数据...据我所知,这必须由您自己完成,因为查询/更新 ...是的,它可以使用 uniqueId 来完成

标签: javascript arrays angularjs


【解决方案1】:

我假设您可以检索要更新的相应数据的索引。 所以,你可以试试。

dataList.splice(index, 1);
dataList.splice(index, 0, newItem);

【讨论】:

  • 感谢@user3667973 / HasH。我会试一试,但很想知道 Angular 是否提供了更简单的方法来做到这一点。将用我发现的内容更新这篇文章。
  • 我错过了什么,我不明白为什么 dataList[index] = newItem 不是答案?
【解决方案2】:
$scope.setActive = function(user) {
  User.get({ id: user._id }, function(user){
      user.active = true;
      user.$update(function(user){
        angular.forEach($scope.users, function(u, i) {
          if (u._id === user._id ) {
            $scope.users[i] = user;
          }
        });
      });
    });
};

【讨论】:

    【解决方案3】:

    Angular 视图会在其模型更改时自动更新,因此无需做额外的事情,只需在控制器中更新您的模型即可-

    $scope.data = ajaxdata // data you received from server
    

    在你看来

    <ul>
      <li ng-repeat="item in data">{{item}}</li>
    </ul>
    

    为了更新您的数据,您应该有一些唯一的键值对(在这种情况下为 u_id),如以下代码示例

    $scope.items = [{
        "status": "New",
        "priority_summary": "High",
        "u_id" : 1
    }, {
        "status": "New",
        "priority_summary": "High",
        "u_id" : 2
    }, {
        "status": "New",
        "priority_summary": "High",
        "u_id" : 3
    }, {
        "status": "New",
        "priority_summary": "High",
        "u_id" : 4
    }];
    

    【讨论】:

    • 你是说我应该将数据重新绑定到$scope,而不是尝试在数组中查找项目并更新其属性?
    • angular 提供 2-way 数据绑定,因此当您更新模型时,您的视图将使用新数据呈现。
    • 也许我没有正确解释这一点。我的 Angular 模型有 50 项,第 35 项更改为网页外部。此时,我需要找到第 35 项并更新它。我的问题是弄清楚如何找到第 35 项并对其进行更新。 Push、Pop 等典型的 Angular 方法是插入,而不是更新。这说明清楚了吗?谢谢。
    • 你可以发布你使用的对象数组/对象对象的结构吗?
    • @DevIntern 你的服务器只返回更新数据还是所有数据?以及您使用哪种方法来检索数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 2015-05-19
    • 2018-06-30
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多