使用表单获取用户输入并将该数据共享到另一个组件中以显示它

时间:2019-04-11 08:00:19

标签: angular typescript

我有一个表单字段,用于接收用户的姓名,电子邮件,主题输入。现在,如何将这些数据共享到同一模块的不同组件以显示它。

<div class="jumbotron">
  <form [formGroup]="suggestForm" (ngSubmit)="onSubmit(suggestForm.value)">
      <div class="form-group">
          <input type="text" formControlName="name" class="form-control" id="exampleInputName"  placeholder="Full Name" [ngClass]="{ 'is-invalid': submitted && f.name.errors }">
          <div *ngIf="submitted && f.name.errors" class="invalid-feedback">
              <div *ngIf="f.name.errors.required">Name is required</div>
          </div>
        </div>
  <div class="form-group">
    <input type="email" formControlName="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email" [ngClass]="{ 'is-invalid': submitted && f.email.errors }">
    <div *ngIf="submitted && f.email.errors" class="invalid-feedback">
        <div *ngIf="f.email.errors.required">Email is required</div>
        <div *ngIf="f.email.errors.email">Email must be a valid email address</div>
    </div>
  </div>
  <div class="form-group">
      <input type="text" formControlName="subject" class="form-control" id="exampleInputSubject"  placeholder="What's the subject?" [ngClass]="{ 'is-invalid': submitted && f.subject.errors }">
      <div *ngIf="submitted && f.subject.errors" class="invalid-feedback">
          <div *ngIf="f.subject.errors.required">Subject is required</div>
      </div>
  </div>
  <div class="form-group">
      <textarea type="text" formControlName="story" class="form-control" id="exampleInputStory"  placeholder="What's the story?" [ngClass]="{ 'is-invalid': submitted && f.story.errors }">
          <div *ngIf="submitted && f.story.errors" class="invalid-feedback">
              <div *ngIf="f.story.errors.required">Story is required</div>
          </div>
      </textarea>
  </div>

  <button type="submit" class="btn btn-primary">Submit</button>
</form>

上面是用于表单字段的用户输入

    result: SuggestForm;
    suggestForm: FormGroup;
    submitted = false;

    ngOnInit() {
      this.suggestForm = this.formBuilder.group({
        name: ['', Validators.required],
        email: ['', [Validators.required, Validators.email]],
        subject: ['', Validators.required],
        story: ['', Validators.required]
      });
    }

    get f() { return this.suggestForm.controls; }

    onSubmit() {
      this.submitted = true;

      if (this.suggestForm.invalid) {
        return;
      }

      alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.suggestForm.value));
      result = Object.assign({}, this.suggestForm.value);
      console.log(result);
    }


上面的代码是angular。中的component.ts文件

export class SuggestForm {
    name: string;
    email: string;
    subject: string;
    story: string;
  }

上面是RecommendationForm.ts类

如何在另一个组件中使用该结果对象来显示输入值

1 个答案:

答案 0 :(得分:0)

SuggestForm.ts

import 'MessageService' from './messageService.ts'
result: SuggestForm;
suggestForm: FormGroup;
submitted = false;

constructor(private msgSer: MessageService)
ngOnInit() {
  this.suggestForm = this.formBuilder.group({
    name: ['', Validators.required],
    email: ['', [Validators.required, Validators.email]],
    subject: ['', Validators.required],
    story: ['', Validators.required]
  });
}

get f() { return this.suggestForm.controls; }

onSubmit() {
  this.submitted = true;

  if (this.suggestForm.invalid) {
    return;
  }

  alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.suggestForm.value));
  result = Object.assign({}, this.suggestForm.value);
  this.msgSer.setMessage(result);
  console.log(result);
}

为示例消息服务创建新服务

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()

export class MessageService {
public message = new Subject<string>();
setMessage(value: string) {
   this.message.next(value); //it is publishing this value to all the subscribers 
   that have already subscribed to this message
  }
}

还有其他订阅该主题的组件,因此,每当您设置结果时,这些组件都会获得最新的价值。

coponentOne.ts

   ngOninit() {
    this.messageService.message.subscribe((message) => {
        console.log(message);
    });
   }

coponentTwo.ts

   ngOninit() {
    this.messageService.message.subscribe((message) => {
        console.log(message);
    });
   }

我希望它能对您有所帮助。