在阵列角更新对象 [英] Angular update object in array

查看:81
本文介绍了在阵列角更新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要更新一个对象数组中的对象。是否有另一种可能性比遍历所有项目,并更新其是匹配的人吗?当前code如下所示:

I wanna update an object within an objects array. Is there another possibility than iterating over all items and update the one which is matching? Current code looks like the following:

angular.module('app').controller('MyController', function($scope) {
    $scope.object = {
        name: 'test',
        objects: [
            {id: 1, name: 'test1'},
            {id: 2, name: 'test2'}
        ]
    };

    $scope.update = function(id, data) {
        var objects = $scope.object.objects;

        for (var i = 0; i < objects.length; i++) {
            if (objects[i].id === id) {
                objects[i] = data;
                break;
            }
        }
    }
});


推荐答案

的过滤器,在从阵列查找元件帮助,也可用于更新,直接在阵列中的元素。
在下面的code [0] - >是直接访问对象

Filters that help in finding the element from the array, can also be used to update the element in the array directly. In the code below [0] --> is the object accessed directly.

Plunker演示 $过滤器(过滤器')
          ($ scope.model,{姓:选择})[0]

这篇关于在阵列角更新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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