以编程方式创建选项卡

时间:2017-08-01 17:01:54

标签: angular angular-material2

我正在使用材料标签here。我很难弄清楚如何以编程方式创建(然后专注于)新标签。我还想删除不再需要的标签。通过挖掘网络,我开始猜测一些函数调用,如

export class VehiclesComponent {

  @ViewChild(MdTabsModule) tabs: MdTabsModule;

  constructor (private router: Router, private parentComponent: AdminComponent, private userService: VehicleService) {}

  ngOnInit() {
    /* code presented here for demonstration purposes */
    this.tabs.push({label: "Some new tab", content: '<b>New tab content</b>'});
  }
}

当然这段代码是完全组成的,但不起作用。至少现在我知道MdTabsModule没有名为push的方法。不幸的是,文档非常恐慌。通过TypeScript创建选项卡甚至没有被远程提及;检查源代码也没有显示出这样的功能。

1 个答案:

答案 0 :(得分:2)

应该很容易。您可以在component.html中使用*ngFor

假设你有一个对象MyTabs:

class MyTabs {
    Label: string;
    Contents: string;
}

在组件类中创建此类的数组,例如

myTabs: MyTabs [];

然后在component.html中,迭代myTabs以创建动态标签。

看看这里:https://github.com/angular/material2/blob/master/src/demo-app/tabs/tabs-demo.html