首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 2 FormGroups和语义UI错误

Angular 2 FormGroups和语义UI错误
EN

Stack Overflow用户
提问于 2016-09-03 07:17:29
回答 2查看 325关注 0票数 0

我正在利用Angular CLI和对RC5的支持,并且我正在尝试利用Semantic UI library,并且我正在尝试在我的表单中利用它们的各种表单元素,比如<input>,利用FormGroups。

下面是我的错误:

代码语言:javascript
复制
EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in ./SemanticInputComponent class SemanticInputComponent - inline template:0:19
ORIGINAL EXCEPTION: TypeError: Cannot read property 'valid' of undefined
ORIGINAL STACKTRACE:
TypeError: Cannot read property 'valid' of undefined
    at DebugAppView._View_SemanticInputComponent0.detectChangesInternal (SemanticInputComponent.ngfactory.js:163:54)
    at DebugAppView.AppView.detectChanges (http://localhost:4200/main.bundle.js:64298:14)
    at DebugAppView.detectChanges (http://localhost:4200/main.bundle.js:64404:44)
    at DebugAppView.AppView.detectViewChildrenChanges (http://localhost:4200/main.bundle.js:64324:19)
    at DebugAppView._View_SubmitLinkComponent0.detectChangesInternal (SubmitLinkComponent.ngfactory.js:360:8)
    at DebugAppView.AppView.detectChanges (http://localhost:4200/main.bundle.js:64298:14)
    at DebugAppView.detectChanges (http://localhost:4200/main.bundle.js:64404:44)
    at DebugAppView.AppView.detectViewChildrenChanges (http://localhost:4200/main.bundle.js:64324:19)
    at DebugAppView._View_SubmitLinkComponent_Host0.detectChangesInternal (SubmitLinkComponent.ngfactory.js:37:8)
    at DebugAppView.AppView.detectChanges (http://localhost:4200/main.bundle.js:64298:14)
ERROR CONTEXT:
[object Object]

这是我的表格:

代码语言:javascript
复制
<form [formGroup]="submitLinkForm" novalidate (ngSubmit)="onSubmit(submitLinkForm.value, submitLinkForm.valid)">
      <ul>
        <li>
          <label>Title</label>
          <sm-input [control]="title" class="left" placeholder="Enter title..."></sm-input>
          <small [hidden]="submitLinkForm.controls.title.valid || (submitLinkForm.controls.title.pristine && !submitted)">
              Title is required
          </small>
        </li>
        <li><input type="submit" value="Submit" /></li>
      </ul>
    </form>

这是我的组件(简化版)

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { Validators } from '@angular/common';
import { REACTIVE_FORM_DIRECTIVES, FormGroup, FormControl, FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-submit-link',
  templateUrl: './submit-link.component.html',
  styleUrls: ['./submit-link.component.scss'],
  directives: [REACTIVE_FORM_DIRECTIVES]
})
export class SubmitLinkComponent implements OnInit {
  public submitLinkForm: FormGroup; // our model driven form
  public submitted: boolean; // keep track on whether form is submitted
  public events: any[] = []; // use later to display form changes

  constructor(
    private _fb: FormBuilder
  ) { }

  ngOnInit() {
    this.submitLinkForm = this._fb.group({
      title: ['', [<any>Validators.required]],
    });
  }

  onSubmit(data, isValid) {

    if (isValid) {}
  }
}

有人知道我为什么要写这篇文章吗?它似乎与输入字段上的[control]属性有关。

EN

回答 2

Stack Overflow用户

发布于 2016-09-03 07:32:50

我需要将输入字段更改为以下内容,从而解决了此问题:

<sm-input [control]="submitLinkForm.controls.title" class="left" placeholder="Enter title..."></sm-input>

看起来,控件属性需要访问实际的FormGroup控件

票数 0
EN

Stack Overflow用户

发布于 2016-09-03 20:23:48

使用这个

代码语言:javascript
复制
  <small [hidden]="submitLinkForm.controls['title'].valid || (submitLinkForm.controls['title'].pristine && !submitted)">
          Title is required
      </small>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39301784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档