在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
2024-03-27 20:03

满意答案

如果您有一个名为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

相关问答

更多

Symfony2嵌入了一系列表单(Symfony2 Embed a Collection of Forms)

如果硬件上有ManyToOne ,则MagazineStockTakingDetails会引用类型为Hardware的对象,因此您只能设置一个类型为硬件的对象,但您正在尝试设置ArrayCollection 。 这不起作用: ->add('hardware','collection',array( 'type' => new HardwareFormType(), 'allow_add' => true, 'by_refere...

在Symfony2中嵌入表单集合时出错(Error when embed a collection of Forms in Symfony2)

由于AddressType类中的这一行而发生此错误: ->add('address', TextType::class, array('label' => 'Adresse*', 'required' => true)) 您正在尝试访问Address实体的address属性,但它不包含它。 这与其他领域相关: CodePostal => postalCode等 同样在你的Shop实体你有一个注释为OneToMany关系的address字段,这是正常的,同时你有setAddress(ArrayCol...

symfony2手动嵌入表单(symfony2 embed forms manually)

如果您有一个名为IngredientType的表单,您可以将其嵌入父表单中,如下所示: $builder->add('ingredients', new IngredientType()); 这对于可以多次重复使用的表单部分非常有用(即联系信息字段,在几种类型的表单中可能相同) 这是一个更全面的例子: http : //symfony.com/doc/current/cookbook/form/form_collections.html If you have a form called Ing...

Symfony嵌入式表单 - 可能不显示嵌入式表单的“父”标签?(Symfony embedded form - possible to NOT show embedded form's “parent” label?)

这是禁用所有标签的简单方法。 如果频繁使用,请将此方法添加到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)

您可以按如下方式访问上传的资产: $imageThumbFile = $form->get('imageThumb')->get('file')->getData(); $imageLargeFile = $form->get('imageLarge')->get('file')->getData(); 如果您打算将这些存储在数据库中作为blob,那么您可能会发现这个SO问题很有用: 如何使用Doctrine持久保存图像 或者,您可以将上载的文件存储在文件系统中,并按照菜谱条目如何使用Doctr...

在Symfony2中嵌入一个集合类型(Embed a collection type in Symfony2)

你说,你的data_class是一个BenefitGroup $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\BenefitGroup', )); 然后设置一个BenefitItem 。 $BI = new BenefitItem(); // ... $form = $this->createForm(new BenefitItemFormType(), $BI); 那不行。 我没有检查你的其余代码,但这也...

在Symfony中手动创建表单(Creating a form manually in Symfony)

根据1.1或1.2 sfForm是一个独立的库(类),除非您从Doctrine / Propel sfForm扩展,否则它与您的数据库没有连接。 因为它是独立的,你可以按照你的意愿使用它,你只需要创建一个表单 类AddProductForm扩展了sfForm { } 并使用一个隐藏的小部件进行设置,该小部件将保存您的product_id和一个product_count文本输入,该输入应该是用户想要订购的产品数量。 然后,只需执行标准if($ form-> isValid()),然后将其插入到数据库...

Symfony2嵌入了所有FOSUserBundle表单(Symfony2 embed all FOSUserBundle forms)

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)

在我的表单中发现错误,因此可以选择此类表单: 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)

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, Fo...

相关文章

更多

Angular实践----前言与概览

前言: 最近在在工作中需要开发一个类似微信公众平台的webapp后台管理应用,在框架的选者上面,之前已 ...

Struts2标签select的使用

我想在进入系统首页的时候就对数据库进行查询,然后讲要查询的数据以下拉里表的形式放在页面上 Actio ...

HTML5 Drag and Drop【HTML5教程 - 第六篇】

拖放/拖拽(drag and drop)这是个非常普遍的功能。你可以抓住一个对象,并且拖动到你想放置的 ...

源码解读Mybatis List列表In查询实现的注意事项

源码解读Mybatis List列表In查询实现的注意事项 在SQL开发过程中,动 ...

Python 列表(list)操作

列表就像java里的collection,所具有的特性也要比元组更多,更灵活,其character总结 ...

Select2在Bootstrap 3 Modal框中不能搜索的解决方法

在项目中用了Select2,基于Bootstrap 3的搜索下拉框。但奇怪的是,在modal-dial ...

List用完后需要赋null吗

从数据库查询N条记录放在List集合中,然后通过request对象返回给页面,通过循环遍历将List中 ...

hibernate 对list修改

class A{ private String a; private String b; pri ...

FreeMarker集合(List、Map)

我们上一节认识了FreeMarker基本数据类型,接口认识FreeMarker集合(List、Map) ...

java List排序一

java List对象排序有多种方法,下面是其中两种 第一种方法,list中的对象实现Comparab ...

最新问答

更多

如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)

请尝试以下方法: $messages = $user->contact_messages()->paginate(10); Try the following: $messages = $user->contact_messages()->paginate(10);

linux的常用命令干什么用的

linux和win7不一样,win7都图形界面,都是用鼠标来操作打开,解压,或者关闭。而linux是没有图形界面的,就和命令提示符一样的一个文本框,操作linux里的文件可没有鼠标给你用,打开文件夹或者解压文件之类的操作都要通过linux常用命令。

由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)

它创建了这些视图: 'auth/login.blade.php', 'auth/register.blade.php', 'auth/passwords/email.blade.php', 'auth/passwords/reset.blade.php', 'layouts/app.blade.php', 'home.blade.php' 并修改这些文件: 'Http/Controllers/HomeController.php', 'routes/web.php' 如果使用--views

如何交换返回集中的行?(How to swap rows in a return set?)

您可以使用特殊的CASE表达式进行ORDER BY如下所示: SELECT * FROM table ORDER BY CASE id WHEN 3 THEN 8 WHEN 8 THEN 3 ELSE id END You can ORDER BY using a special CASE expression like this: SELECT * FROM table ORDER BY CASE id WHEN 3 THEN 8 WHEN 8 THEN 3 ELSE id END

在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)

我建议你制作一个新的TableView Cell,在这个newTableViewCell中,加载第2节中的所有单元格,然后为newTableViewCell提供边框。 但如果您不想这样做,那么您可以使用以下代码: @property(strong,nonatomic) CAShapeLayer* borderPath; -(void)viewDidLayoutSubviews{ [_borderPath removeFromSuperlayer]; UIView *viewToGiveBord

使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)

出于一些奇怪的原因,我现在发现了一个不同的问题, Boost.Spirit SQL语法/词法分析失败 ,其中提供了一些其他解决方案来进行空格跳过。 一个更好的! 以下是根据建议重新编写的示例代码: #include #include #include #include #include #incl

Java中的不可变类(Immutable class in Java)

1.如果我只有final变量的课程? 这会让你远离但不是全部。 这些变量的类型也需要是不可变的。 考虑一下 class MyImmutableClass { // final variable, referring to a mutable type final String[] arr = { "hello" }; // ... } 这允许有人做 myImmutableObject.arr[0] = "world"; 并有效地改变你的不可变类的对象。 此外,建议禁

WordPress发布查询(WordPress post query)

如果你想要在div中包含所有帖子: post_parent) { $myposts = get_posts('numberposts=10&tag=medical&order=DESC'); echo ' '; foreach ($myposts as $post): ?>

如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)

我不确定哪个是MySQL的正确答案,因为它本身还不支持IPv6地址格式(尽管“ WL#798:MySQL IPv6支持 ”表明它将在MySQL v6.0中,当前文档不支持)。 不过,你建议的人建议去2 * BIGINT,但要确保他们是UNSIGNED。 在IPv6的/ 64地址边界处有一种自然分割(因为/ 64是最小的网格块大小),这将与其很好地对齐。 I'm not sure which is the right answer for MySQL given that it doesn't y

是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)

您可以使用Object.keys和Array#find来获取匹配value的key 。 const letters = {a:'26',b:'25',c:'24',d:'23',e:'22',f:'21',g:'20',h:'19',i:'18',j:'17',k:'16',l:'15',m:'14',n:'13',o:'12',p:'11',q:'10',r:'9',s:'8',t:'7',u:'6',v:'5',w:'4',x:'3',y:'2',z:'1'}; function sw