出现意外的EOF字符错误(呈角度形式),为什么?

时间:2018-10-04 12:01:42

标签: angular typescript angular6 mean-stack angular-reactive-forms

我已经使用Form Builder创建了此表单,但是在控制台中抛出了错误。

错误:

compiler.js:1021未捕获的错误:模板解析错误: 意外字符“ EOF”(“ rity.value,负责任的值”)mat-raised-button color =“ primary”>保存   

代码:

<mat-card>
  <form [formGroup]="createForm" class="create-form">
    <mat-form-field class="field-full-width">
      <input matInput placeholder="Title" formControlName="title" #title>
    </mat-form-field>
    <mat-form-field class="field-full-width">
      <input matInput placeholder="Responsible" formControlName="responsible" #responsible>
    </mat-form-field>
    <mat-form-field class="field-full-width">
        <textarea matInput placeholder="Description" formControlName="description" #description>
    </mat-form-field>
    <mat-form-field class="field-full-width">
       <mat-select placeholder="Severity" formControlName="severity" #severity>
         <mat-option value="low">Low</mat-option>
         <mat-option value="medium">Medium</mat-option>
         <mat-option value="high">High</mat-option>
       </mat-select>
    </mat-form-field>
    <mat-divider></mat-divider>
    <br>
    <br>
    <button mat-raised-button color="accent" routerLink="/list">Back</button>
    <button type="submit" (click)="addIssue(title.value, description.value, severity.value, responsible.value)" mat-raised-button color="primary">Save</button>
  </form>
</mat-card>

.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import {IssueService} from '../../../issue.service';

@Component({
  selector: 'app-create',
  templateUrl: './create.component.html',
  styleUrls: ['./create.component.css']
})
export class CreateComponent implements OnInit {

  createForm: FormGroup;

  constructor(private Issue: IssueService, private router: Router, private fb: FormBuilder) {
      this.createForm = this.fb.group({
        title: '',
        description: '',
        severity: '',
        responsible: ''
      });
  }

  addIssue(title, description, responsible, severity) {
      this.Issue.addIssue(title, description, severity, responsible).subscribe(() => {
        console.log('Issue added');
        this.router.navigate(['/list']);
      });
  }

  ngOnInit() {
  }

}

2 个答案:

答案 0 :(得分:0)

这样做的原因是您忘记关闭textarea标签。

 <textarea ... formControlName="description" #description></textarea>
                                                             /\
                                                             ||
                                                          add this 

Angular遵循HTML规范规定的相同限制

答案 1 :(得分:0)

您在选择垫子时出错。您不能在mat-select上创建模板变量。 我不明白您为什么要传递这样的价值观。不建议在ReactiveForms中使用此方法。 用以下内容替换您的html:

<mat-card>
  <form [formGroup]="createForm" class="create-form">
    <mat-form-field class="field-full-width">
      <input matInput placeholder="Title" formControlName="title">
    </mat-form-field>
    <mat-form-field class="field-full-width">
      <input matInput placeholder="Responsible" formControlName="responsible">
    </mat-form-field>
    <mat-form-field class="field-full-width">
        <textarea matInput placeholder="Description" formControlName="description">
    </mat-form-field>
    <mat-form-field class="field-full-width">
       <mat-select placeholder="Severity" formControlName="severity">
         <mat-option value="low">Low</mat-option>
         <mat-option value="medium">Medium</mat-option>
         <mat-option value="high">High</mat-option>
       </mat-select>
    </mat-form-field>
    <mat-divider></mat-divider>
    <br>
    <br>
    <button mat-raised-button color="accent" routerLink="/list">Back</button>
    <button type="submit" (click)="addIssue()" mat-raised-button color="primary">Save</button>
  </form>
</mat-card>

使用以下内容替换您的组件:

从'@ angular / core'导入{Component,OnInit};

import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import {IssueService} from '../../../issue.service';

@Component({
  selector: 'app-create',
  templateUrl: './create.component.html',
  styleUrls: ['./create.component.css']
})
export class CreateComponent implements OnInit {

  createForm: FormGroup;

  constructor(private Issue: IssueService, private router: Router, private fb: FormBuilder) {
      this.createForm = this.fb.group({
        title: '',
        description: '',
        severity: '',
        responsible: ''
      });
  }

  addIssue() {
      this.Issue.addIssue(...this.createForm.value).subscribe(() => {
        console.log('Issue added');
        this.router.navigate(['/list']);
      });
  }

  ngOnInit() {
  }

}