当用户使用材质对话框注册时,以角度重新加载特定组件

时间:2018-07-31 03:28:39

标签: angular angular5 angular2-routing angular-material2

我正在尝试将注册用户加载到上进行注册的位置。一旦用户注册,就应该在注册用户中填充。为此,我编写了以下代码。

下面的代码在projects.component.html

<a href="javascript:;" (click)="openDialog()">Register</a>

下面的代码在projects.component.ts

openDialog(): void {
    const dialogRef = this.dialog.open(RegisterComponent, {
      width:'50%',
        height:'85%'
    });
  }

当我单击“注册”时,将打开一个包含以下字段的对话框。下面的代码是register.component.html

<form role="form" #heroForm="ngForm">

            <!-- Registration Form on Modal Dialog Box Start -->

            <!-- Name Start -->
            <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)]="entry.name" name="name" id="name"
                             matInput placeholder="Name">
                  </mat-input-container>
              </div>
          </div>
          <!-- Name End -->

          <!-- Conditional Select Field for Different Account Types. Eg. Git, Bitbucket, GitLab, Github -->
          <div class="form-group row">
            <label class="col-md-0 control-label">
            </label>
            <div class="col-md-5">
                <mat-form-field>
                    <mat-select placeholder="Account Type" required [(ngModel)]="entry.accountType"
                                name="type">
                        <mat-option *ngFor="let type of accountTypes" [value]="type">
                            {{type}}
                        </mat-option>
                    </mat-select>
                </mat-form-field>
            </div>
        </div>
        <!-- Conditional Select Field for Different Account Types. Eg. Git, Bitbucket, GitLab, Github End -->             

              <!-- Git  Start    -->
              <div class="form-group row" *ngIf="entry.accountType === 'Git'">
                <label for="gitAccessKey" class="col-md-0 control-label">
                </label>
                <div  class="col-md-5">
                    <mat-input-container class="full-width" *ngIf="entry.accountType != 'Slack'" >
                        <input required [(ngModel)]="entry.accessKey" name="accessKey" id="gitAccessKey"
                               matInput type="text" placeholder="Access-Key">
                    </mat-input-container>
                </div>
                </div>
            </div>

            <div class="form-group row" *ngIf="entry.accountType === 'Git'">
                <label for="gitSecret" class="col-md-0 control-label">
                </label>
                <div class="col-md-5">
                    <mat-input-container class="full-width" >
                        <input required [(ngModel)]="entry.secretKey" name="secret" id="gitSecret"
                               matInput type="password" placeholder="Secret-Key">
                    </mat-input-container>
                </div>
            </div>
            <!-- Git  End    -->
 </form>

下面的代码用于register.component.ts

import {Component, OnInit, Inject} from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';
import {Routes, RouterModule, Router, ActivatedRoute}from "@angular/router";
import {AccountService}from '../../../services/account.service';
import {OrgService}from '../../../services/org.service';
import {SnackbarService}from '../../../services/snackbar.service';
import {Account} from '../../../models/account.model';
import {Handler}from '../../dialogs/handler/handler';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.scss'],
  providers: [AccountService, OrgService, SnackbarService]
})
export class RegisterComponent implements OnInit{
  entry: Account = new Account();
  accountTypes = ['Git', 'GitHub', 'BitBucket', 'GitLab'];
  constructor(private accountService: AccountService, 
    private orgService: OrgService,
    private handler: Handler, 
    private snackbarService: SnackbarService,
    private route: ActivatedRoute, 
    private router: Router,
    public dialogRef: MatDialogRef<RegisterComponent>,
    private dialog: MatDialog) {}

  ngOnInit() {
  }

  create() {
    this.handler.activateLoader();
    this.snackbarService.openSnackBar(this.entry.name + " registering...", "");
    this.accountService.create(this.entry).subscribe(results => {
      this.handler.hideLoader();
      if (this.handler.handle(results)) {
        return;
      }
    this.snackbarService.openSnackBar(this.entry.name + " registered successfully", "");
    this.entry = results['data'];
    console.log(this.entry);
    this.onClose();
    //this.refresh();
    this.router.navigate(['/app/projects/new']);    
    }, error => {
      this.handler.hideLoader();
      this.handler.error(error);
    });
  }

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

  refresh() {
    location.reload()
  }
}

请参见下图,以获得更好的清晰度。所以我的工作流程是这样的,当用户在对话框中注册自己时,应使用angular在下拉列表中填充注册的用户名。我用过window.location.reload();但问题在于它会完全重新加载整个页面。这是糟糕的用户体验。因此,在不重新加载整个页面的情况下,还有其他任何可能的方式可以使用户在同一条路线上,而不会分散他从当前页面上的注意力?有什么建议吗?意味着我只想重新加载该特定视图。

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

这是SPA和AJAX应用程序的全部要点,您不必每次都重新加载页面。在这种情况下也是一样。

您可以将所有用户存储在一个集合中。然后,当您从服务器确认用户已正确登录时,可以引发一个事件,将该帐户推送到列表中。然后,如果列表中的所有项目都绑定到此集合,则视图将自动更新。

这里是一个例子:

select.component.html

<mat-select placeholder="Favorite food" [(ngModel)]="selectedValue" name="item">
  <mat-option *ngFor="let item of foods" [value]="item.value">
    {{item.viewValue}}
  </mat-option>
</mat-select>

<button mat-raised-button (click)="addItem()">Add item</button>

select.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-select',
  templateUrl: './select.component.html',
  styleUrls: ['./select.component.css']
})


export class SelectComponent implements OnInit {

  foods: Food[] = [
    { value: 'food-0', viewValue: 'Pizza' },
    { value: 'food-1', viewValue: 'Steak' },
    { value: 'food-2', viewValue: 'Fries' },
  ];
  constructor() { }

  ngOnInit() {
  }

  addItem() {
    this.foods.push({value: 'food', viewValue: 'Hamburgers'});
    console.log(`Items count ${this.foods.length}`);
  }

}

export interface Food {
  value: string;
  viewValue: string;
}