【问题标题】:FormControl in Angular 2Angular 2 中的 FormControl
【发布时间】:2016-11-05 20:21:43
【问题描述】:

我问了这个问题:Analogue of Angular 2 Provider

但是在将 angular 更新到 rc.4 并将 @angular/forms 更新到 0.2.0 之后,它就不再起作用了。

部分模板:

    <form [formGroup]="formGroup">
        ...
        <checkbox-view  [formControl]="formGroup.find('rememberMe')">Remember me?</checkbox-view>
    </form>

在组件中:

this.formGroup = this._fromBuilder.group({
     ...
     rememberMe: [this.model.rememberMe]
});

还有错误: ''没有值访问器

【问题讨论】:

  • @GünterZöchbauer 我在 PLATFORM_DIRECTIVES 上更改了 NG_VALUE_ACCESSOR 但没有帮助
  • disableDeprecatedForms() 呢?
  • @GünterZöchbauer 谢谢。在做 plunker 的时候发现了一个 bug =)
  • @dibs 我使用 useExisting: forwardRef(() => CheckboxComponent 这样的结构是错误的。你应该需要使用 useExisting: CheckboxComponent 就是这样

标签: angular angular2-template angular2-forms angular2-formbuilder


【解决方案1】:

属性控件有一个包含所有控件的地图。

你可以这样做:

<form [formGroup]="formGroup">
    ...
    <checkbox-view  [formControl]="formGroup.controls['rememberMe']">Remember me?</checkbox-view>
</form>

【讨论】:

    【解决方案2】:
       Try using formControlName without the square brackets instead of formControl.
    
        <form [formGroup]="formGroup">
            ...
            <checkbox-view  formControlName="rememberMe">Remember me?  </checkbox-view>
        </form>
    
       If you have nested fields you can also do this..
    
        <form [formGroup]="formGroup">
            ...
           <div formGroupName="topFieldName">
            <checkbox-view  formControlName="subFieldName">Remember me?  </checkbox-view>
           </div>
        </form>
    

    【讨论】:

    • 还有其他问题。您可以阅读问题的 cmets =)
    【解决方案3】:

    在最新的 Angular 2 版本中

    <form [formGroup]="formName">
       <checkbox-view  formControlName="rememberMe">Remember me</checkbox-view>
    </form>
    
    
    this.formName = this._fromBuilder.group({
         rememberMe: [this.model.rememberMe]
    });
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    猜你喜欢
    • 2018-05-02
    • 2017-01-28
    • 2017-12-01
    • 2018-06-23
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多