【问题标题】:How to update an arrays (angularjs) element without splice?如何在没有拼接的情况下更新数组(angularjs)元素?
【发布时间】:2015-09-16 01:55:28
【问题描述】:

我在这篇帖子here 中遇到了类似的情况,只是我不仅需要获取元素,还需要更改它,例如名称值。

我已经发现可以这样做:

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

但是有几个问题。我知道 id 但如果我不时操作数组,我将失去对索引 id 相关性的跟踪,因为使用这种方法我会取出项目,更改它们并将它们作为“新”推送,对?但这有点不太优雅,我认为可能会导致问题。

基本上我只想切换一个可见的属性,然后应该在数组中更改。这是数组:

$scope.cLines = [{ id: 1, cColor: 'red', cName: 'Entryline right', visible: true }];

当然里面通常有更多的元素,但为了简单起见,我留下了一个。

可见的切换器应该是这样工作的(天真的“伪代码”,如果它能像那样简单地工作,那就太棒了:))

$scope.cLines[id === id].visible = !$scope.cLines[id === id].visible;

如果我可以直接使用过滤器访问元素,那么第二好的事情是,这可能吗?

提前谢谢你。

【问题讨论】:

  • 你是如何触发可见性变化的?
  • 看看 angular extend docs.angularjs.org/api/ng/function/angular.extend,可能会有帮助
  • 页面上有一个按钮,它执行我想将参数设置为相反的功能(真/假)。基于此,ng-show 要么显示定义元素,要么不显示。我正在研究该扩展功能,但似乎没有帮助。谢谢

标签: javascript arrays json angularjs


【解决方案1】:

有几种方法可以解决这个问题。一种是使用filter()

var id = 1;
var visibility = true;

var items = $scope.cLines.filter(function(item) {
    return item.id === id;
});
if (items.length >= 1) items[0].visible = visibility;

你可以把它包装成一个函数:

function setVisibility(arr, id, visibility) {
    var items = arr.filter(function(item) {
        return item.id === id;
    });
    if (items.length >= 1) items[0].visible = visibility;
}

然后像这样使用它:

setVisibility($scope.cLines, 1, true);

您还可以将$scope.cLines 更新为更复杂的对象,而不仅仅是一个数组:

$scope.cLines = {
    "item" : function (id) {
        var items = this.lines.filter(function(item) {
            return item.id === id;
        });
        if (items.length >= 1) 
            return items[0];
        else
            return new Object(); //or throw an error
    },
    "lines" : [
        { id: 1, cColor: 'red', cName: 'Entryline right', visible: true }
        //....and more
    ]
};

然后像这样使用它:

$scope.cLines.item(1).visible = true;

有了这个,如果你必须循环访问它,请确保使用$scope.cLines.lines

【讨论】:

    【解决方案2】:

    我不确定我是否完全理解了这个问题,所以如果我离开这里,也许你可以澄清你想要做什么。

    如果您有一个 ng-repeat 并且您试图切换当前对象中的某个值,只需将该对象传递给 ng-click 函数:

    <button ng-click="changeVisible(line);" ng-repeat="line in cLines">Visible = {{line.visible}}</button>
    

    然后在你的控制器中,你会有这样的东西:

    $scope.changeVisible = function(obj) {
        obj.visible = !obj.visible;
      }
    

    var app = angular.module('demo', []);
    
    app.controller('DemoCtrl', function($scope) {
      $scope.cLines = [{
        id: 1,
        cColor: 'red',
        cName: 'Entryline right',
        visible: true
      }, {
        id: 2,
        cColor: 'blue',
        cName: 'Entryline right',
        visible: false
      }];
    
      $scope.changeVisible = function(obj) {
        obj.visible = !obj.visible;
      }
    
    });
    <script src="https://code.angularjs.org/1.3.16/angular.js"></script>
    <div ng-app="demo">
      <div ng-controller="DemoCtrl">
        <button ng-click="changeVisible(line);" ng-repeat="line in cLines">Visible = {{line.visible}}</button>
      </div>
    </div>

    【讨论】:

    • 我以前也是这样,但是如果我想改名怎么办?或者如果我只想在控制器中“以编程方式”更改它?
    • 它是一个对象,所以如果你想改变一个属性值,只需告诉它新的值应该是什么。在上面的示例中,您可以将函数更改为:obj.name = "some new name"。如果你想添加一个全新的属性,只需定义它:obj.mynewproperty = "newval"。我不确定您以编程方式更改它是什么意思。也许你可以给我一个例子。
    猜你喜欢
    • 2023-01-31
    • 2020-06-26
    • 2021-12-05
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2012-09-26
    • 2016-07-06
    • 1970-01-01
    相关资源
    最近更新 更多