我有div,需要在特定的路径(如下所述)上显示它。谢谢!
路径
{ path: 'preview/:questionid', component: QuestionPreviewComponent, outlet: 'questions' }
http://localhost:3000/admin/questions/(questions:preview/5cb18f686560a2f98b43f8e0)
路由模块
imports: [RouterModule.forChild([
{ path: '', redirectTo: 'questions', pathMatch: 'full'},
{ path: 'questions', component: QuestionsComponent, children: [
{ path: '', component: QuestionListComponent, outlet: 'questions' },
{ path: 'bulk-upload', component: BulkUploadQuestionsComponent, outlet: 'questions' },
{ path: 'new', component: NewEditQuestionComponent, outlet: 'questions' },
{ path: 'edit/:id', component: NewEditQuestionComponent, outlet: 'questions' },
{ path: 'preview/:questionid', component: QuestionPreviewComponent, outlet: 'questions' }
]}
])],
我尝试了这个,但是没有用。
<div style="margin-left:3% ;" *ngIf="router.url == 'questions/preview/:questionid'">
.....code.....
</div>
答案 0 :(得分:0)
您需要从questionid
获取ActivatedRoute
,然后在您的*ngIf
条件下使用它
questionid: string;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.route.params.subscribe(params => {
this.questionid = params['questionid']; // Save Question Id in a variable
});
}
在模板中使用上述变量。
<div style="margin-left:3% ;" *ngIf="router.url == ('/admin/questions/preview/' + questionid)">
.....code.....
</div>