相关疑难解决方法(0)

Angular4 - 表单控件没有值访问器

我对自己的表格有点疑惑.

我做了一个自定义元素:

<div formControlName="surveyType">
  <div *ngFor="let type of surveyTypes"
       (click)="onSelectType(type)"
       [class.selected]="type === selectedType">
    <md-icon>{{ type.icon }}</md-icon>
    <span>{{ type.description }}</span>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我尝试添加formControlName但有角度不想知道任何事情,只是说:

<div formControlName="surveyType">
  <div *ngFor="let type of surveyTypes"
       (click)="onSelectType(type)"
       [class.selected]="type === selectedType">
    <md-icon>{{ type.icon }}</md-icon>
    <span>{{ type.description }}</span>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我试图添加ngDefaultControl但没有成功.这似乎是因为没有输入/选择...但我不知道该怎么做.

我想将我的点击绑定到这个formControl,以便当有人点击整个卡片时将我的'type'推入formControl.可能吗?

javascript typescript angular angular4-forms

124
推荐指数
3
解决办法
14万
查看次数

将Angular反应式FormControl传递给子组件

我有一个父组件,我想在其中创建和存储我的反应式表单。表单数组中将有多个表单组实例。我想将表单控件从每个表单组传递给子组件,但是在弄清楚如何做到这一点时遇到了麻烦。

这是一个Stackblitz,演示了我想做的事。另外,某些字段仅适用于汽车的某些“品牌”,这就是为什么我的html中包含以下行:

<input type="text" *ngIf="car.make != 'ford'" formControlName="model">
Run Code Online (Sandbox Code Playgroud)

我想将“详细信息”表单组字段移到“详细信息字段”子组件中,但是当我尝试这样做时,它告诉我我没有表单组实例,所以我知道父表单组是未在子组件中注册。任何帮助深表感谢。

components angular-components angular angular-reactive-forms angular-forms

8
推荐指数
1
解决办法
6834
查看次数

如何解决使用大量自定义组件创建复杂表单的问题?

假设我从angular2应用程序生成的html如下所示:

<app>
<form [formGroup]="myForm" (ngSubmit)="onSubmit(myForm.value)">
<panel-component>
    <mid-component>
        <inner-component-with-inputs>
            <input/>
        <inner-component-with-inputs>
    <mid-component>
</panel-component>
<panel-component>
    <mid-component>
        <inner-component-with-inputs>
            <input/>
        <inner-component-with-inputs>
    <mid-component>
</panel-component>

<!-- many many many fields -->

<button type="submit">Submit</button>
</form>
</app>
Run Code Online (Sandbox Code Playgroud)

如何设置我的外部<form>以便我可以在提交时验证所有内部输入?我必须通过myForm通过@Input()从一路下跌panel-componentinner-component-with-inputs?或者还有其他方式吗?

在我的应用程序中,我有一个非常大的形式,有多个面板,子面板,标签,模态等.我需要能够在提交时一次性验证它.

互联网上的所有教程和资源仅涉及跨越一个组件/模板的表单.

typescript angular2-forms angular2-template angular

7
推荐指数
1
解决办法
2198
查看次数

如何在子组件中使用formGroupName

如何在子组件中使用formGroupName?例如:

我有ParentFormComponent

    parentForm: FormGroup;
    constructor(private fb: FormBuilder, private http: Http) { }
    ngOnInit() {


 this.parentForm = this.fb.group({
        _general: this.fb.group ({
           ProjectName:''
       })
  })
 }
Run Code Online (Sandbox Code Playgroud)

在html中:

  <form [formGroup]="parentForm" (ngSubmit)="submitForm()">
     <div formGroupName="_general">
        <mat-form-field>
             <input matInput placeholder="Project name" 
             formControlName="ProjectName">
        </mat-form-field>
    </div> 
  </form> 
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但是当我想使用子组件时,它不起作用:

<form [formGroup]="parentForm" (ngSubmit)="submitForm()">
          <app-child [parentForm]='parentForm'></app-child>
      </form> 
Run Code Online (Sandbox Code Playgroud)

当我将其插入子组件时:

<div formGroupName="_general">
            <mat-form-field>
                 <input matInput placeholder="Project name" 
                 formControlName="ProjectName">
            </mat-form-field>
        </div> 
Run Code Online (Sandbox Code Playgroud)

并在ts文件中

 @Input() parentForm:FormGroup;
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:formGroupName必须与父formGroup指令一起使用。您需要添加一个formGroup指令,并将其传递给现有的FormGroup实例(您可以在类中创建一个)。

angular angular-reactive-forms angular6

6
推荐指数
1
解决办法
2744
查看次数