简体   繁体   English

在选择中选择要添加的字段并将其添加

[英]Select the field to be added in the select and add it

I have select with options of fields. 我选择了字段选项。 I select the field I want to add. 我选择要添加的字段。 When I call a function, I add an object to the array. 调用函数时,将一个对象添加到数组中。 From this object you need to get the field. 从该对象中,您需要获取字段。 It turned out. 结果是。

    $scope.addNewField = function(id, type) {
      console.log(type);
      if(type == "DD"){
        $scope.arrays.push({
                  "id":$scope.arrays.length + 1,
                  "parentId":id,
                  "type":"DD",
                  "images":[],
                  "text":[
                    {
                      "title":"",
                      "text":""
                    }
                  ],
                  "table":[]
                });

        $scope.indexarr = $scope.arrays.length - 1
              console.log($scope.arrays);

              var html='<div ng-click="selectedValue(value)">Value</div>',
                  el = document.getElementById('myID');

                  angular.element(el).append( $compile('<div class="form-group" dragula="bag-one"><label for="inputEmail3" class="col-sm-1 control-label">Название</label><div class="col-sm-10"><input type="text" class="form-control" ng-model="arrays[indexarr].text[0].text" name="title" placeholder="Название" required><p ng-show="userForm.name.$error.required && ( vm.formSubmitted || userForm.name.$touched)" class="help-block">Это обязательное поле.</p></div></div>')($scope) )


      }
      if(type == "TABLE"){
        $scope.arrays.push({
                  "id":3,
                  "parentId":id,
                  "type":"TABLE",
                  "images":[],
                  "text":[
                    {
                      "title":null,
                      "text":"Andrey For WebView"
                    }
                  ],
                  "table":[]
                });
                     $scope.indexarr = $scope.arrays.length - 1
              console.log($scope.arrays);

              var html='<div ng-click="selectedValue(value)">Value</div>',
                  el = document.getElementById('myID');

                  angular.element(el).append( $compile('<div class="form-group" dragula="bag-one"><label for="inputEmail3" class="col-sm-1 control-label">Название</label><div class="col-sm-10"><input type="text" class="form-control" ng-model="arrays[indexarr].text[0].text" name="title" placeholder="Название" required><p ng-show="userForm.name.$error.required && ( vm.formSubmitted || userForm.name.$touched)" class="help-block">Это обязательное поле.</p></div></div>')($scope) )


      }
}

HTML: HTML:

      <select ng-model="test"><option value="DD">DD</option><option value="TABLE">TABLE</option></select>
      <button type="button" ng-click="addNewField(1, test)">отправить</button>

I tried ng-repeat but it duplicates field ng-IF does not help. 我尝试了ng-repeat,但是重复字段ng-IF却无济于事。

Must be so: 必须这样:

-Select field -选择字段

-Add field and add array in $scope. -添加字段并在$ scope中添加数组。

I think, you are missing the element in your HTML to which you are trying to add the fields dynamically. 我认为,您正在丢失要动态添加字段的HTML中的元素。

Try inserting below line somewhere in your HTML body and see if it works. 尝试将以下行插入HTML正文中的某处,看看是否可行。

<div id="myID"></div>

Suggestion is based on below two lines from your code. 建议基于代码中的以下两行。 Let me know how it goes. 让我知道事情的后续。

el = document.getElementById('myID');
angular.element(el).append( $compile('<div class="form-group" dragula="bag-one"><label for="inputEmail3" class="col-sm-1 control-label">Название</label><div class="col-sm-10"><input type="text" class="form-control" ng-model="arrays[indexarr].text[0].text" name="title" placeholder="Название" required><p ng-show="userForm.name.$error.required && ( vm.formSubmitted || userForm.name.$touched)" class="help-block">Это обязательное поле.</p></div></div>')($scope) )

Thank you all. 谢谢你们。 Solved the problem with the help of ng-switch 借助ng-switch解决了该问题

Example: 例:

                  <div class="containter" dragula='"another-bag"' dragula-model='arrays'>

                <div ng-repeat="item in arrays" dragula='"bag-one"'>

                  <div ng-switch on="item.type" id="mySwitch">

                        <div ng-switch-when="DD" class="entry-photo">
                        <div class="form-group" >
                          <label for="inputEmail3" class="col-sm-1 control-label">Название</label>
                          <div class="col-sm-10">
                            <input type="text" class="form-control" ng-model="item.text[0].text" name="title" placeholder="Название" required>
                            <p ng-show="userForm.name.$error.required && ( vm.formSubmitted || userForm.name.$touched)" class="help-block">Это обязательное поле.</p>
                          </div>
                        </div>
                      </div>   


                      <div ng-switch-when="TABLE" class="entry-photo">
                        <div class="form-group">
                          <label for="inputEmail3" class="col-sm-1 control-label">Название</label>
                          <div class="col-sm-10">
                            <input type="text" class="form-control" ng-model="item.text[0].text" name="title" placeholder="Название" required>
                            <p ng-show="userForm.name.$error.required && ( vm.formSubmitted || userForm.name.$touched)" class="help-block">Это обязательное поле.</p>
                          </div>
                        </div>
                      </div> 


                  </div>

                  </div>
            </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM