简体   繁体   English

Angular中的动态标签:在动态标签内容中导航

[英]Dynamic tabs in Angular : navigation in dynamic tab content

I'm developing a demo app that contain a search bar, and with each search action a tab should be opened.我正在开发一个包含搜索栏的演示应用程序,每个搜索操作都应打开一个选项卡。

I succeeded to do that following this awesome article https://juristr.com/blog/2017/07/ng2-dynamic-tab-component/在这篇很棒的文章https://juristr.com/blog/2017/07/ng2-dynamic-tab-component/之后,我成功地做到了这一点

The requirement is that each dynamic tab has his own context as the following images show要求是每个动态选项卡都有自己的上下文,如下图所示在此处输入图像描述

在此处输入图像描述

Now inside the tab content I need to have navigation between different components.现在在选项卡内容中,我需要在不同组件之间进行导航。

I don't think that routing can be used, because as I mentioned, each tab has his own context.我不认为可以使用路由,因为正如我提到的,每个选项卡都有自己的上下文。

what is the appropriate way to do so?这样做的适当方法是什么? Any tutorials, any hint, or a place to start from would be very helpful ( I'm a beginner with angular )任何教程、任何提示或起点都会非常有帮助(我是 angular 的初学者)

Ok.行。 If i understand correctly i think this could be one approach.如果我理解正确,我认为这可能是一种方法。

I tried to thik at something simple...我试着想一些简单的事情......

In you tab content you write the html code like this:在您的选项卡内容中,您可以像这样编写 html 代码:

<div class="listContent" *ngIf="showDetail==false">
<!-- Populate this list as you are doing -->
<ul>
    <li>List</li>
    <li>Of</li>
    <li>Items</li>
</ul>
</div>

<div class="detailContent" *ngIf="showDetail==true">
  <!-- Put here the detail to show after the item click -->
</div>

In your typescript file you can keep the state of the content to show through a boolean variable, in this case i used showDetail .在您的 typescript 文件中,您可以保留内容的 state 以通过 boolean 变量显示,在本例中我使用了showDetail Then at the click of an item of the list you switch this variable, so the content will be shown.然后在单击列表中的一个项目时切换此变量,以便显示内容。

You component should have a function like this:你的组件应该有一个 function 这样的:

  showDetail: boolean = false;
  
  switchContent(){
     //Set here all the variables
     // and the content you want to show

     showDetail = true
  }

Don't forget to have a "go back" button or something like that, so you can set showDetail = false and return to the previous list不要忘记有一个“返回”按钮或类似的东西,这样你就可以设置showDetail = false并返回到上一个列表

More complex way更复杂的方式

If you are not already using <router-outlet> you can place it inside the tab content.如果您还没有使用<router-outlet>您可以将它放在选项卡内容中。 Then you create two components (one for displaying the list anc one for the detail) and then you navigate through them using routing.然后创建两个组件(一个用于显示列表,一个用于显示详细信息),然后使用路由在它们之间导航。 For details on routing see: https://angular.io/guide/router#defining-a-basic-route有关路由的详细信息,请参阅: https://angular.io/guide/router#defining-a-basic-route

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM