【问题标题】:create ng-model dynamically in ng-repeat and child ng-repeat using $index and $parent.$index?使用 $index 和 $parent.$index 在 ng-repeat 和子 ng-repeat 中动态创建 ng-model?
【发布时间】:2017-02-11 00:14:06
【问题描述】:

当我单击保存并添加另一个项目按钮时,我有一些输入字段和复选框的表单控制器,我一次复制(克隆)整个表单控制器。可以做n次。当我单击按钮时,我正在创建一个数组推送表单元素

 <div class="row category-description-block" ng-repeat="values in categoryval.CategoryValuesArray track by $index">
            <div class="col">
                <h3>Tell us about your jewelry</h3>
                <div class="list">
           <div class="form_group">
                        <label class="item item-input item-select">
                            <div class="input-label">
                                Type of Jewelry?
                            </div>
                              <span class="input-label"></span>
                            <input ng-model="categoryval.Luxary_Eyeware_Brand[$index]" type="text" placeholder="">
                        </label>
                    </div>




                    <!-- more details about jewelry starts -->
                <ion-checkbox ng-model="categoryval.jewelrymoreDetailschecked[$index]" ng-change="jewelrymoreDetailsChange($index)">I have the jewelry details
                        <br/> (Diamond shape, clarity, color, etc.)</ion-checkbox>

                    <div ng-show="IsVisible[$index]">

                        <div>
                            <div class="row">
                                <div class="col">
                                    <h4>Describe your diamonds</h4>
                                </div>
                            </div>



                            <div class="repeat-set card">
                                <!-- inner repeating div strats-->

                                <div class="repeat-div" ng-repeat="jewels in categoryval.moreJewelryDetailsArray track by $index">
                                    <div class="row close-icon-bg">
                                        <div class="col">
                                            <a href="" ng-model="jewels.closeItem[$parent.Sindex][$index]" ng-click="removeItem($parent.$index,$index)" class="close-icon">remove</a>
                                        </div>
                                    </div>
                                    <div class="row ">
                                        <div class="col">
                                            <div class="form_group">
                                                <label class="item item-input item-stacked-label">
                                                    <span class="input-label">Quantity </span>
                                                    <input ng-model="jewels.quantity[$parent.$index][$index]" type="text" placeholder="Quantity">
                                                </label>

                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <!-- inner repeating div Ends-->



                                <div class="form_group text-center">
                                    <label class="item item-input item-stacked-label">
                                        <button class="button button-small button-royal" ng-click="addJewelryDetails($index)">Add an additional item</button>
                                    </label>
                                </div>


                    <div class="form_group">
                        <label class="item item-input item-stacked-label">
                            <span class="input-label">Description</span>
                            <textarea ng-model="categoryval.description[$index]" placeholder=""></textarea>
                            <p class="form-notes">The more information you provide, the better!</p>
                        </label>
                    </div>



                </div>
            </div>
        </div>



        <div class="row custom-button">
            <div class="col-25">
                <button class="button button-block button-balanced" ng-click="categoryContinue(categoryval)">Continue</button>
            </div>
            <div class="col">
                <button class="button button-block button-balanced" ng-click="globalAppend()">Save and add another item</button>
            </div>
        </div>
    </div>

在里面的表单控制器中,当我单击复选框时,我有一个复选框,有一个名为数量的表单输入字段并添加另一个项目按钮。您可以使用它来创建 n 个数量。我正在使用另一个 ng-重复将值推入另一个数组。

 $scope.addJewelryDetails = newJewelryDetails;
$scope.globalAppend = newGlobalDiv;


$scope.IsVisible = [];
$scope.categoryval = {
    jewelrymoreDetailschecked: false
};

$scope.jewelrymoreDetailsChange = function(index) {
    $scope.IsVisible[index] = $scope.categoryval.jewelrymoreDetailschecked[index];

};


$scope.categoryContinue = function(categoryvalues) {
    console.log(categoryvalues);
};



       $scope.categoryval = {};
     // $scope.categoryval.jewelryCollection = {};
    $scope.categoryval.images = [];
     $scope.categoryval.CategoryValuesArray = [];
      $scope.categoryval.moreJewelryDetailsArray = [];
      $scope.categoryval.quantity = [];

function init() {

    newJewelryDetails();
    newGlobalDiv();
}

function newGlobalDiv() {

    $scope.categoryval.CategoryValuesArray.push({});
}

function newJewelryDetails(parentindex) {

    $scope.categoryval.moreJewelryDetailsArray.push({});
    $scope.quantity = '';

}
$scope.removeItem = function(parentindex,index) {
    console.log(JSON.stringify(parentindex));

   $log.log("parent Index:::" + parentindex + "index::::" +  index);


  //var quantity =       $scope.categoryval.moreJewelryDetailsArray[index].quantity;     
   $scope.categoryval.moreJewelryDetailsArray.splice(index,1);

};  


init();

但我对数量字段添加和删除有疑问。例如 Parent1 有 3 个 Quantities 字段,而 parent2 有 3 个 quantity 。每个父母都有独立的数量。当我从 parent2 中删除第二个数量时,它也会从 parent 1 中删除。我只想单独删除父 2 和 parent1 并添加数量字段。我完整的工作小提琴在这里codepen。任何人为我提供解决方案提前谢谢

【问题讨论】:

    标签: angularjs dynamic ionic-framework ng-repeat angular-ngmodel


    【解决方案1】:

    不要将多维数组用于这种逻辑。 Javascript/Json 非常适合这项工作。

    你应该有一个包含项目列表的珠宝数组:

    [
      {
        items: [{name: "", description: ""}, {name: "", description: ""}]
      },
      {
        items: [{name: "", description: ""}, {name: "", description: ""}]
      }
    ]
    

    您以同样的方式删除项目:

    $scope.removeItem = function(parentindex,index) {
       $scope.jewelryArray[parentIndex].items.splice(index,1);
    };
    

    概念证明:https://plnkr.co/edit/PLTFDvHszXrTWA7rrjcf?p=preview

    编辑:根据 cmets 完成 plunker。

    https://plnkr.co/edit/crLzfRKFA2FrhRhXLcGY?p=preview

    【讨论】:

    • 所以我想为父子创建两个数组对吗?你能举个例子吗
    • @Ben10 我添加了一个 plunker
    • 所以每个表单字段都创建为一个数组,比如珠宝类型作为数组,数量也作为数组对吗?
    • @Ben10 是的,除了数量。数量应该是数组的大小或对象的属性。项目:[{名称:“”,描述:“”,数量:123},{名称:“”,描述:“”,数量:51}]
    • 如何定义ng-model值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多