我有一个包含许多字段的表单组:
this.addressForm = this.formBuilder.group({
line1: ['', Validators.required],
line2: '',
line3: '',
line4: '',
line5: '',
line6: '',
line7: '',
line8: '',
});
Run Code Online (Sandbox Code Playgroud)
在我的 html 中,每个 formControl 都有一个表单字段,附近有一个按钮可以清除该表单控件。
<mat-form-field>
<mat-label>line 1</mat-label>
<input matInput formControlName="line1" type="text">
<button type="button" (click)="clearLine('line1')">
</button>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
如何编写一个通用方法来获取表单控件的名称并清除它?
我试试这个——
clearLine(line) {
this.addressForm.patchValue({line: ''});
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为它搜索了 formControl 名称“line”。
有没有办法在不执行许多“if”条件的情况下做到这一点?
我有以下路线-
const routes: Routes = [
{
path: '',
component: ParentComponent,
children: [
{
path: ':id',
component: ChildComponent,
},
]
}
];
Run Code Online (Sandbox Code Playgroud)
在父的构造函数中,我想导航到某个 id,例如“test”。我试过这个-
constructor(private router: Router) {
this.router.navigate(['child']);
}
Run Code Online (Sandbox Code Playgroud)
和这个-
constructor(private router: Router) {
this.router.navigate(['/child']);
}
Run Code Online (Sandbox Code Playgroud)
和这个-
constructor(private router: Router) {
this.router.navigate(['./child']);
}
Run Code Online (Sandbox Code Playgroud)
和这个-
constructor(private router: Router) {
this.router.navigate(['../child']);
}
Run Code Online (Sandbox Code Playgroud)
我的网址是 - http://localhost:4200/parent。
但所有这些选项我都去http://localhost:4200/child而不是http://localhost:4200/parent/child
知道为什么吗?我怎样才能使它相对于父母?
我定义了一个延迟加载模块。
这是SettingsRoutingModule模块-
const routes: Routes = [
{
path: '',
component: SettingsStandaloneComponent,
children: [
{
path: '',
redirectTo: 'profile',
pathMatch: 'full'
},
{
path: 'profile',
component: SettingsProfileComponent
},
{
path: 'about',
component: SettingsAboutComponent
},
{
path: 'affiliations',
component: SettingsAffiliationsComponent
},
{
path: 'communication',
component: SettingsCommunicationComponent
},
{
path: 'notifications',
component: SettingsNotificationsComponent
},
{
path: 'proxies',
component: SettingsProxiesComponent
},
{
path: '**',
redirectTo: 'profile',
pathMatch: 'full'
}
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class SettingsRoutingModule { } …Run Code Online (Sandbox Code Playgroud)