你如何更新一个角度阵列的现有项目(已外部更改)? [英] How do you update an existing item in an Angular Array (that has changed externally)?

查看:131
本文介绍了你如何更新一个角度阵列的现有项目(已外部更改)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的角度和我有(不通过角供电UI)更新我的角度阵列的现有项目已外部更改挣扎。

I am new to Angular and am struggling with updating an existing item in my Angular array that has changed externally (not via Angular powered UI).

下面是用例...我的网页,通过服务器端调用填充,我加载数组角度并显示在列表上。

Here is the use case...My web page is populated via a server side call and I am loading the array into Angular and displaying on a list.

现在,如果在服务器上的变化和新的记录中的数据插入到表中,我的网页的JavaScript被告知,它通过推(参考成功地插入一个新的记录到角阵。<一个href=\"http://stackoverflow.com/questions/23860072/programmatically-inserting-array-values-in-angular-js\">Programmatically在角JS 插入阵列值)。

Now, if the data on the server changes and a new record is inserted in the table, my page's JavaScript is notified and it successfully inserts a new records into the Angular array via 'push' (Ref. Programmatically inserting array values in Angular JS).

然而,当现有的记录被改变了我的网页也被通知(在服务器端/不通过角供电UI)。我画一个关于我如何去了解我的角度阵列更新正确的记录空白?有没有办法,我在角文档已经错过了一个查询/更新的方法?

However, my page is also notified when an existing record is changed (on the server side / not via Angular powered UI). I am drawing a blank about how do I go about updating the correct record in my Angular array? Is there a query / update method that I have missed in the Angular docs?

下面是我目前的code样子...

Here is what my current code looks like...

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

这凸显任何建议或例子将是巨大的。谢谢!

Any suggestions or examples highlighting this would be great. Thanks!

推荐答案

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

I am assuming that you can retrieve the index of corresponding data you want to update. So, you can try.

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

这篇关于你如何更新一个角度阵列的现有项目(已外部更改)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆