【问题标题】:Select2 function is not working in angular js dynamic formSelect2 函数不能以 Angular js 动态形式工作
【发布时间】:2018-09-18 05:20:50
【问题描述】:

大家好,我正在从事 Angular js 项目。但是这里有一个小问题,我正在对多个字段使用动态表单并使用 select2 函数进行搜索,但是第一次它可以工作,然后在创建新字段后它不能用于搜索,你能帮帮我吗??

这是我制作动态字段的控制器代码

$scope.choices = [{id: 1,purchaser_account:'',price:0,weight:0}];
$scope.addNewChoice = function() {
var newItemNo = $scope.choices.length+1;
   $scope.choices.push({'id':newItemNo,purchaser_account:'',price:0,weight:0});
};

这里是视图代码

<div  data-ng-repeat="choice in choices">
                        <div class="row">                           
                          <div class="col-md-3">
                            <div class="form-group label-floating">
                                <label class="control-label urdu-lable"> خریدار کھاتہ</label>
                                <select ng-model="choice.purchaser_account" name="account{{choice.id}}" class="form-control select2" required>
                                <option ng-repeat="x in purchaserAccount" value="{{x.id}}">{{x.name}}</option>
                                </select>
                            </div>
                         </div>
                            <div class="col-md-2">
                                <div class="form-group label-floating">
                                    <label class="control-label urdu-lable" > وزن</label>
                                    <input type="text" id="weight" ng-model="choice.weight"  class="form-control" required="required">
                                </div>
                            </div>
                            <div class="col-md-2">
                                <div class="form-group label-floating">
                                    <label class="control-label urdu-lable"> ریٹ</label>
                                    <input type="text" id="price" ng-model="choice.price"  class="form-control" required="required">
                                </div>
                            </div>

                            <div class="col-md-2">
                                <div class="form-group label-floating">
                                    <label class="control-label urdu-lable"> ٹوٹل</label>
                                    <div>{{((choice.price*(choice.weight/40))+((choice.price*(choice.weight/40))*.016)).toFixed(2)}}
                                    </div>
                                </div>
                            </div>


                            <div class="col-md-1">
                                <div class="form-group label-floating">
                                    <button class="remove" ng-show="$last" ng-click="removeChoice()">-</button>
                                </div>
                            </div>
                        </div>                    
                    </div>
<button class="btn btn-info" ng-click="addNewChoice()">Add fields</button>    

你能帮我解决这个问题吗

【问题讨论】:

    标签: javascript angularjs jquery-select2


    【解决方案1】:

    在您的角度添加功能上初始化选择。

    $scope.choices = [{id: 1,purchaser_account:'',price:0,weight:0}];
    $scope.addNewChoice = function() {
    var newItemNo = $scope.choices.length+1;
       $scope.choices.push({'id':newItemNo,purchaser_account:'',price:0,weight:0});
       $(".select2").select2();
    };
    

    在选择数组中添加第一个选项的位置同样初始化它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 2012-05-09
      • 2013-08-25
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 2018-08-28
      相关资源
      最近更新 更多