简体   繁体   English

如何使用anuglar js在按钮单击上创建动态输入字段?

[英]How to create a Dynamic input fields on button click using anuglar js?

I have a dropdown menu like this 我有一个这样的下拉菜单

在此处输入图片说明

[ShortAnswer- TextBox,Checkbox,Radio button and Drop down] User can choose the input type and there can add the options by clicking the add option button like this [ShortAnswer-文本框,复选框,单选按钮和下拉菜单]用户可以选择输入类型,然后可以通过单击添加选项按钮来添加选项,如下所示 在此处输入图片说明

by clicking the cross icon the options need to be removed. 通过单击十字图标,需要删除选项。

I reffered dynamic text area 我引用了动态文本区域

http://viralpatel.net/blogs/dynamic-add-textbox-input-button-radio-element-html-javascript/

But I need more reference about this, Please Suggest me an idea to achieve this using angular js. 但是我需要更多有关此的参考,请提出一个使用angular js实现此目的的想法。

Build your list of inputs from an array of objects that each define an input. 从每个定义输入的对象数组构建输入列表。 On button click add a new object to the array that defines the input you want to add. 在按钮上,单击将新对象添加到定义要添加的输入的数组。

html: 的HTML:

<div ng-repeat="input in inputsList">
  <span ng-click="remove($index)>x</span><input ng-if="input.type==='text'" type="text" />
  <span ng-click="remove($index)>x</span><input ng-if="input.type==='checkbox'" type="checkbox" />
  ...
</div>
<button ng-click="addNewCheckBox()">Add</button>

controller: 控制器:

$scope.addNewCheckBox = function() {
  $scope.inputsList.push({ type: 'checkbox' });
};

$scope.remove = function(index) {
   $scope.inputsList.splice(index, 1);
};

It would probably be best if you had a custom input directive that took a config object and it would decide which input to build. 如果您有一个带配置对象的自定义输入指令,它可以决定要构建哪个输入,那可能是最好的。 But for the sake of simplicity and clarity I just used basic raw html inputs. 但是为了简单明了,我只使用了基本的原始html输入。

Let me know if that makes sense or you need an Angular2/4 answer. 让我知道这是否有意义,或者您需要Angular2 / 4答案。

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

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