angular - 当用户使用材质对话框注册时以角度重新加载特定组件
问题描述
我正在尝试将注册用户加载到 . 用户注册后,应将注册用户填充到 .为此,我编写了如下代码。
下面的代码在 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(); 但它的问题是它完全重新加载了整个页面。这是一个糟糕的用户体验。因此,如果不重新加载整个页面,是否还有其他可能性可以让用户走在同一条路线上,而不会分散他对当前页面的注意力?请问有什么建议吗?意味着我只想重新加载那个特定的视图。
解决方案
这是 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>
选择.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;
}
推荐阅读
- spring-boot - 具有相同参数值的休眠查询与线程睡眠但得到相同的结果虽然数据库更新行值
- c# - ASP.NET 请求中的 async/await 有什么好处?
- excel - Excel - 我希望将行分组在一起并创建这些分组行的表格,以便按特定列对它们进行排序
- xcode - 在 XCode 中归档 React Native 项目时出现未定义符号错误
- odbc - Apache Superset 是否可以使用来自 ODBC 连接的数据?
- html - React Microfronends:如何将身份验证和其他信息从一个前端应用程序传递到另一个前端应用程序?
- logback - 为什么这个 logback 配置不写入任何文件附加器?
- django - 在 AWS EC2 上托管 Django 站点时不存储任何 cookie
- sql - 无法从 BLOB 存储打开备份设备 - 访问被拒绝
- html - 我解析了html。创建 CheckFilter 函数,如果函数返回 true,如何转换到 View1,如果返回 false,则转换到 view2。帮助