javascript – 使用角度2实现标签

前端之家收集整理的这篇文章主要介绍了javascript – 使用角度2实现标签前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
>我试图以角度方式显示 jquery选项卡.
>所以我创建了一个方法tabsUL(),我试图在警报中显示值,但我没有得到它,
>之后我不知道该怎么办.
>在下面提供我的jquery codepen
>提供下面没有工作的plnkr
>你们能告诉我怎么办吗?

工作小提琴https://codepen.io/texirv/pen/Vzdqpo

不工作的小提琴
http://plnkr.co/edit/XMLRIGEJLnxK3Vv9Y8Ma?p=preview

  1. export class App {
  2. constructor() {
  3. this.name = 'Angular2'
  4. }
  5.  
  6. tabsUL(): void {
  7. //console.log("I am here");
  8. //alert("I am here");
  9. var tab_id = $(this).attr('data-tab');
  10. alert("tab_id------>" + tab_id);
  11. }
  12. }

解决方法

我找到的最简单的方法是通过 this post,它工作正常,有角度方式,易于扩展:

tab.component.ts

  1. import { Component,Input } from '@angular/core';
  2.  
  3. @Component({
  4. selector: 'tab',styles: [`
  5. .pane{
  6. padding: 1em;
  7. }
  8. `],template: `
  9. <div [hidden]="!active" class="pane">
  10. <ng-content></ng-content>
  11. </div>
  12. `
  13. })
  14. export class TabComponent {
  15. @Input('tabTitle') title: string;
  16. @Input() active = false;
  17. }

tabs.component.ts

  1. import { Component,ContentChildren,QueryList,AfterContentInit } from '@angular/core';
  2. import { TabComponent } from './tab.component';
  3.  
  4. @Component({
  5. selector: 'tabs',template:`
  6. <ul class="nav nav-pills nav-fill">
  7. <li class="nav-item" *ngFor="let tab of tabs" (click)="selectTab(tab)">
  8. <a class="nav-link" tabindex [class.active]="tab.active">{{tab.title}}</a>
  9. </li>
  10. </ul>
  11. <ng-content></ng-content>
  12. `
  13. })
  14. export class TabsComponent implements AfterContentInit {
  15.  
  16. @ContentChildren(TabComponent) tabs: QueryList<TabComponent>;
  17.  
  18. // contentChildren are set
  19. ngAfterContentInit() {
  20. // get all active tabs
  21. let activeTabs = this.tabs.filter((tab)=>tab.active);
  22.  
  23. // if there is no active tab set,activate the first
  24. if(activeTabs.length === 0) {
  25. this.selectTab(this.tabs.first);
  26. }
  27. }
  28.  
  29. selectTab(tab: TabComponent){
  30. // deactivate all tabs
  31. this.tabs.toArray().forEach(tab => tab.active = false);
  32.  
  33. // activate the tab the user has clicked on.
  34. tab.active = true;
  35. }
  36.  
  37. }

your.component.html

  1. <tabs #tabs>
  2. <tab #foo tabTitle="foo">
  3. tab foo content
  4. </tab>
  5. <tab #bar tabTitle="bar">
  6. tab bar content
  7. </tab>
  8. </tabs>

猜你在找的JavaScript相关文章