【问题标题】:how to remove nested form in angularJS using splice如何使用拼接删除angularJS中的嵌套形式
【发布时间】:2017-10-02 02:29:14
【问题描述】:

我有一个嵌套表单....我可以在其中添加新字段,但在删除字段时遇到了一些问题。

我希望能够拼接我的对象数组中的特定位置以删除字段

目前我有...

html:

<div ng-repeat="senarioItem in attendees.formData.scenarios[0].scenarioItems" class="row">
        <div ng-hide="cost._destroy">    
            <div class="form-group col-xs-5">
                <label for="costDescription">Description</label>
                <input class="form-control input-group-lg" type="text" name="costDescription" ng-model="senarioItem.costDescription"/>
            </div>
            <div class="form-group col-xs-2">
                <label for="cost">Amount</label>
                <input 
                    class="form-control input-group-lg" 
                    type="number" 
                    name="cost"
                    ng-model="senarioItem.cost"/>
            </div>

            <div class="col-md-2">
                <a ng-click="removeCost()" class="btn btn-xs btn-danger">X</a>
            </div>
        </div>
    </div>

控制器:

attendees.removeCost = function(){
            var cost = attendees.formData.scenarios[0].scenarioItems[index];
            if(cost.id) {
                cost._destroy = true;
            } else {
                attendees.cost.splice(index, 1);
            }


        var cost = attendees.formData.scenarios[0].scenarioItems[index];
    };

JSON:

"scenarioItems": [
        {
          "cost": "",
          "costDescription": "",
        },
        {
          "cost": "",
          "costDescription": ""
        },
        {
          "cost": "",
          "costDescription": ""
        },
        {
          "cost": "",
          "costDescription": ""
        }
      ]

【问题讨论】:

    标签: angularjs forms nested-forms splice


    【解决方案1】:

    将索引作为参数传递给函数

    <div class="col-md-2">
       <a ng-click="removeCost($index)" class="btn btn-xs btn-danger">X</a>
    </div>
    

    然后在函数中删除它

    attendees.removeCost = function(index){
                var cost = attendees.formData.scenarios[0].scenarioItems[index];
                if(cost.id) {
                    cost._destroy = true;
                } else {
                    attendees.cost.splice(index, 1);
                }
    
    
            var cost = attendees.formData.scenarios[0].scenarioItems[index];
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-06
      • 2013-06-26
      • 2022-12-21
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多