AngularJs - 更改属性值

时间:2015-10-28 10:04:00

标签: angularjs

我是anuglar Js的新手。我想知道如何更改属性值。

 $scope.dashboards = {
                        "1": {
                            "widgets": [{
                                "row": 0,
                                "col": 0,
                                "sizeX": 2,
                                "sizeY": 1,
                                "name": "Canvas 1",
                                "canvas": "canvas_1",
                                "show": true
                            }, {
                                "row": 0,
                                "col": 2,
                                "sizeX": 2,
                                "sizeY": 1,
                                "name": "Canvas 2",
                                "canvas": "canvas_2",
                                "show": true
                            }]
                            }
                     }

在ng-click上,我想将键“show”的值更改为false。

<div gridster-item="widget" ng-repeat="widget in dashboard.widgets">
    <i ng-click="changeValue(widget)" class="glyphicon glyphicon-trash">
</div>

$scope.changeValue= function (widget) {
            .....How to??
        };

4 个答案:

答案 0 :(得分:2)

由于您已将当前窗口小部件作为参数传递,因此您可以执行此类操作

$scope.changeValue= function (widget) {
        widget.show = false;
    };

答案 1 :(得分:2)

我不会传递小部件。相反,我会通过它的$ index:

<i ng-click="changeValue($index)" class="glyphicon glyphicon-trash">

然后:

$scope.changeValue= function (index) {
   $scope.dashboards.1.widgets[index].show = ...
};

答案 2 :(得分:0)

看起来你的ng-repeat中的数组在范围上是不确定的。 您应该更新您的代码,如下所示;

<div gridster-item="widget" ng-repeat="widget in dashboards.1.widgets">
    <i ng-click="changeValue(widget)" class="glyphicon glyphicon-trash">
</div>

获得相关对象并更新$scope中的show属性,如下所示:

$scope.changeValue= function (widget) {
   $scope.dashboards.1.widgets[widget].show = false;      
};

答案 3 :(得分:0)

简单地说,

$scope.changevalue= function(widget){
  for (var i = 0; i < $scope.dashboards.length; i++) {
    for (var j = 0; j < $scope.dashboards[i].widgets.length; j++) {
        if ($scope.dashboards[i].widgets[j].ID == widget.ID){
            $scope.dashboards[i].widgets[j].show= false
        }
    };  
  };      
}