仅在特定路径上显示div

时间:2020-05-13 10:03:27

标签: angular

我有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>

1 个答案:

答案 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>