在Angular 4中选择具有反应形式的下拉列表的值(Value of select drop down list with reactive forms in Angular 4)
我使用Angular 4中的ReactiveForms模块创建了一个表单。在我的.ts文件中:
myForm: FormGroup; dataTypes: FormArray; ngOnInit() { this.myForm = new FormGroup({}); this.dataTypes = new FormArray([ new FormControl("A"), new FormControl("B"), new FormControl("C") ]); this.myForm.addControl('dataTypes', this.dataTypes); } onSubmit() { console.log(this.myForm.value); }
并在我的HTML:
<form [formGroup]="myForm" (ngSubmit)=onSubmit()> <select name="datatypes" id="datatypes" formArrayName="dataTypes"> <option *ngFor="let dataType of myForm.get('dataTypes').controls; let dataIndex=index" [ngValue]="dataIndex"> {{ dataType.value }} </option> </select> <button type="submit">Submit</button> </form>
在点击提交按钮时,我试图控制登录提交表单的值。 带有下拉列表的表单显示正确。 第一步是提取用户选择的drop drop列表的值。 但是console.log给出了一个包含所有三个值的数组,而不是所选的值。 myForm.value如何在提交时只有选定的dataTypes值?
I created a form using ReactiveForms module in Angular 4. In my .ts file:
myForm: FormGroup; dataTypes: FormArray; ngOnInit() { this.myForm = new FormGroup({}); this.dataTypes = new FormArray([ new FormControl("A"), new FormControl("B"), new FormControl("C") ]); this.myForm.addControl('dataTypes', this.dataTypes); } onSubmit() { console.log(this.myForm.value); }
And in my html:
<form [formGroup]="myForm" (ngSubmit)=onSubmit()> <select name="datatypes" id="datatypes" formArrayName="dataTypes"> <option *ngFor="let dataType of myForm.get('dataTypes').controls; let dataIndex=index" [ngValue]="dataIndex"> {{ dataType.value }} </option> </select> <button type="submit">Submit</button> </form>
On clicking the submit button, I am trying to console log the value of the form submitted. The form with the drop down list is displayed correctly. The first step is to extract the value of the drop drown list selected by the user. But the console.log gives an array with all the three values and not the value selected. How will the myForm.value have only the selected value of dataTypes on submit?
原文:https://stackoverflow.com/questions/48379209
最满意答案
如果您有一个名为IngredientType的表单,您可以将其嵌入父表单中,如下所示:
$builder->add('ingredients', new IngredientType());
这对于可以多次重复使用的表单部分非常有用(即联系信息字段,在几种类型的表单中可能相同)
这是一个更全面的例子: http : //symfony.com/doc/current/cookbook/form/form_collections.html
If you have a form called IngredientType, you can embed it in a parent form like this:
$builder->add('ingredients', new IngredientType());
This is super helpful for portions of forms that may be reused more than once (i.e. contact info fields, which may be identical in several types of forms)
Here's a more thorough example: http://symfony.com/doc/current/cookbook/form/form_collections.html
相关问答
更多-
如果硬件上有ManyToOne ,则MagazineStockTakingDetails会引用类型为Hardware的对象,因此您只能设置一个类型为硬件的对象,但您正在尝试设置ArrayCollection 。 这不起作用: ->add('hardware','collection',array( 'type' => new HardwareFormType(), 'allow_add' => true, 'by_reference' ...
-
由于AddressType类中的这一行而发生此错误: ->add('address', TextType::class, array('label' => 'Adresse*', 'required' => true)) 您正在尝试访问Address实体的address属性,但它不包含它。 这与其他领域相关: CodePostal => postalCode等 同样在你的Shop实体你有一个注释为OneToMany关系的address字段,这是正常的,同时你有setAddress(ArrayCollecti ...
-
symfony2手动嵌入表单(symfony2 embed forms manually)[2022-02-02]
如果您有一个名为IngredientType的表单,您可以将其嵌入父表单中,如下所示: $builder->add('ingredients', new IngredientType()); 这对于可以多次重复使用的表单部分非常有用(即联系信息字段,在几种类型的表单中可能相同) 这是一个更全面的例子: http : //symfony.com/doc/current/cookbook/form/form_collections.html If you have a form called Ingredie ... -
这是禁用所有标签的简单方法。 如果频繁使用,请将此方法添加到BaseForm 。 public function disableLabels() { $fields = $this->getWidgetSchema()->getFields(); $this->getWidgetSchema()->setLabels(array_combine(array_keys($fields), array_fill(0, count($fields), false))); } 如果您只想禁用嵌入表单中的标 ...
-
Symfony2嵌入式表单(Symfony2 Embedded Forms)[2022-03-19]
您可以按如下方式访问上传的资产: $imageThumbFile = $form->get('imageThumb')->get('file')->getData(); $imageLargeFile = $form->get('imageLarge')->get('file')->getData(); 如果您打算将这些存储在数据库中作为blob,那么您可能会发现这个SO问题很有用: 如何使用Doctrine持久保存图像 或者,您可以将上载的文件存储在文件系统中,并按照菜谱条目如何使用Doctrine处理 ... -
你说,你的data_class是一个BenefitGroup $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\BenefitGroup', )); 然后设置一个BenefitItem 。 $BI = new BenefitItem(); // ... $form = $this->createForm(new BenefitItemFormType(), $BI); 那不行。 我没有检查你的其余代码,但这也是异常所说 ...
-
根据1.1或1.2 sfForm是一个独立的库(类),除非您从Doctrine / Propel sfForm扩展,否则它与您的数据库没有连接。 因为它是独立的,你可以按照你的意愿使用它,你只需要创建一个表单 类AddProductForm扩展了sfForm { } 并使用一个隐藏的小部件进行设置,该小部件将保存您的product_id和一个product_count文本输入,该输入应该是用户想要订购的产品数量。 然后,只需执行标准if($ form-> isValid()),然后将其插入到数据库和/或co ...
-
FOSUserBundle:Resetting:sendEmail期望用户名或电子邮件作为POST。 public function sendEmailAction(Request $request) { $username = $request->request->get('username'); /** @var $user UserInterface */ $user = $this->container->get('fos_user.user_manager') ...
-
Symfony2嵌入表单值(Symfony2 Embed form values)[2022-06-04]
在我的表单中发现错误,因此可以选择此类表单: http : //symfony.com/doc/current/cookbook/form/inherit_data_option.html Found mistake in my form, so there is option for such type of forms: http://symfony.com/doc/current/cookbook/form/inherit_data_option.html -
Symfony2表单文件夹(Symfony2 Form folder)[2023-09-05]
Form文件夹只是一个约定 - 您可以将表单放在任何地方。 该公约延伸至: Form\Type表单类型, Form\Model for form models , Form\Handler程序的Form\Handler , 等等 The Form folder is just a convention — you can put your forms wherever you want. The convention extends to: Form\Type for form types, Form\Mo ...