Angular 5将表单数据同步传递到另一个组件的下拉菜单

时间:2018-08-01 10:19:42

标签: javascript angular typescript

我有一个表格(物料对话框模态),该表格可让用户创建一个帐户,一旦用户单击“注册”按钮,则该用户将使用他创建的该帐户的用户名停留在同一页面上。我遇到的问题是它已被创建,但没有与对话框模态的关闭同步更新。意味着应该注册用户,并在下拉菜单中同时填充创建的用户值,以提高用户体验。我不希望用户分散当前页面的注意力,因为如果用户输入了详细信息并且重新加载了该页面,那么他输入的所有数据都会丢失。所以我不希望这种情况发生。为此,我创建了以下代码。

<section class="container-fluid with-maxwidth chapter">

    <article class="article">
        <div class="box box-default">
            <div class="box-body">

                <mat-horizontal-stepper [linear]="true" #stepper>

                    <mat-step [stepControl]="firstFormGroup">
                        <form [formGroup]="firstFormGroup">
                            <ng-template matStepLabel>Project Registration</ng-template>

                            <div class="form-group row"></div>
                            <div class="form-group row">
                                <label for="name" class="col-md-0 control-label"></label>
                                <div class="col-md-5">
                                    <mat-input-container class="full-width">
                                        <input required [(ngModel)]="project.name" formControlName="nameCtrl" id="name"
                                               matInput placeholder="Project name">
                                    </mat-input-container>
                                </div>
                            </div>

                            <!-- Repository details -->

                            <div class="form-group row">
                                <label for="name" class="col-md-0 control-label"></label>
                                <div class="col-md-5">
                                    <mat-input-container class="full-width">
                                        <input required [(ngModel)]="project.url" formControlName="urlCtrl" id="repo"
                                               matInput placeholder="Repository URL">
                                    </mat-input-container>
                                </div>
                                <div class="col-md-7">
                                    <div class="callout1 text-muted callout-info1">
                                        <p>e.g. https://github.com/username/MyApp.git or
                                            git@github.com:username/MyApp.git.
                                        </p>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group row">
                                <label class="col-md-0 control-label">
                                </label>
                                <div class="col-md-5">
                                    <mat-form-field>
                                        <mat-select placeholder="Repository Credentials" required
                                                    [(ngModel)]="project.account.id" formControlName="typeCtrl">
                                            <mat-option *ngFor="let account of accounts" [value]="account.id">
                                                {{account.name}} ({{account.accountType}})
                                            </mat-option>
                                        </mat-select>
                                    </mat-form-field>
                                </div>
                                <div class="col-md-7">
                                    <div class="callout1 text-muted callout-info1">
                                        <p>
                                            <a href="javascript:;" (click)="openDialog()">Add credentials</a>
                                            <br/>
                                            Credentials are required for automatic test creation and check-in.
                                        </p>
                                    </div>
                                </div>
                            </div>

                            <div>
                                <mat-card-actions>
                                    <button mat-raised-button color="primary"
                                            class="nav-btn"
                                            [disabled]="firstFormGroup.invalid"
                                            (click)="save(stepper);">Save & Next
                                    </button>
                                </mat-card-actions>
                            </div>
<div class="form-group row">
                  <div class="col-md-0"></div>
                  <div class="col-md-8">
                      <button [disabled]="!heroForm.valid" mat-raised-button color="primary" (click)="create();"
                              class="btn-w-md no-margin-left">Register
                      </button>
                      <button mat-button type="button" color="primary" class="btn-w-md" (click)="onClose();">Cancel
                      </button>
                  </div>
              </div>


   </form>
                    </mat-step>
                    </mat-horizontal-stepper>
                    </div>
                    </div>

                    </article>
                    </section>

从模板的以下代码开始,在这里,如果转到其他页面,然后再到此处,则用户名将正确添加到下拉列表中。但是我不希望用户来回查看自己的凭据是否已注册,而是希望显示运行中创建的凭据,以便他可以进一步移动。

<mat-form-field>
                                        <mat-select placeholder="Repository Credentials" required
                                                    [(ngModel)]="project.account.id" formControlName="typeCtrl">
                                            <mat-option *ngFor="let account of accounts" [value]="account.id">
                                                {{account.name}} ({{account.accountType}})
                                            </mat-option>
                                        </mat-select>
                                    </mat-form-field>

注册对话框组件的代码

export class RegisterComponent implements OnInit{
  @Input() entry: Account = new Account();
create(entry: Account) {
    this.handler.activateLoader();
    this.snackbarService.openSnackBar(this.entry.name + " registering...", "");
    this.accountService.create(entry).subscribe(results => {
      this.entry = entry;
      this.handler.hideLoader();
      if (this.handler.handle(results)) {
        return;
      }
    this.snackbarService.openSnackBar(this.entry.name + " registered successfully", "");
    this.onClose();
    // this.router.navigateByUrl('/app/projects', {skipLocationChange: true}).then(()=>
    // this.router.navigate(['/app/projects/new'])); 
    this.router.navigate(['/app/projects/new']);
    }, error => {
      this.handler.hideLoader();
      this.handler.error(error);
    });
  }

  onClose(){
    this.dialogRef.close();
  }
}

这是我编写服务的地方:

import { Account } from './../models/project.model';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Headers } from '@angular/http';
import { Observable, Subject } from 'rxjs';
import 'rxjs/add/observable/timer';
import 'rxjs/add/operator/toPromise';

@Injectable()
export class AccountService {

  private serviceUrl = '/api/v1/accounts';
  private entry = new Subject<Object>();
  entry$ = this.entry.asObservable();
  constructor(private http: HttpClient) {
  }


  create(entry: Account): Observable<any> {
    return this.http.post(this.serviceUrl, entry);
  }

  emitAccount(entry){
    this.entry.next();
  }

}

我希望用户位于同一页面上,并应使用最新结果更新下拉列表。那怎么可能?你能建议我吗?我的代码做错了。

谢谢。

2 个答案:

答案 0 :(得分:0)

创建服务:

let newEntry = this.http.post(this.serviceUrl, entry);
emitAccount(newEntry);
return newEntry;

注册组件:

entry$.subscribe(updateAccount => {
accounts.push(updateAccount)
}

答案 1 :(得分:-1)

要检查..这是关于本节的内容吗?

            <mat-option *ngFor="let account of accounts" [value]="account.id">
                                        {{account.name}} ({{account.accountType}})
                                    </mat-option>

因为注册后我在重新填充帐户数组的代码中看不到任何东西。

我看不到您在哪里绑定accounts数组。