手风琴 https://angular.carbondesignsystem.com/?path=/story/components-accordion—basic

组件及属性

  • ibm-accordion
    • Input
      • align: start|end = end
      • skeleton: boolean = false;
  • ibm-accordion-item
    • Input
      • title: string| TemplateRef;
      • context: Object | null = null;
      • id = accordion-item-${AccordionItem.accordionItemCount}
      • skeleton = false;
    • Output
      • selected

        Tips

        ContentChildren 指令

        使用 ng-content 指令可以实现 Content Projection 内容投影,类似Vue的slot,插槽。如果存在多个 ng-content,可以通过 select 指定要投射的位置。 ``typescript @Component({ selector: 'exe-parent', template:

        Parent Component


        ` })

@Component({ selector: ‘my-app’, template: <h4>Welcome to Angular World</h4> <exe-parent> <exe-child></exe-child> </exe-parent>, })

  1. **ContentChild** 是属性装饰器,用来从通过 Content Projection 方式设置的试图中获取匹配的元素。
  2. **ContentChildren** 属性装饰器,和 ContentChild相比,就是获取匹配的多个元素,返回的结果是一个 QueryList 集合。
  3. **那 ContentChildren ViewChildren 有啥区别呢?**
  4. - 在父组件的开始结束标签中间放入的 元素,成为 ContentChildren.
  5. - 在组件自己的模板中定义的内容,是组件的一部分,称为 ViewChildren.
  6. **ContentChild ViewChild 区别?**
  7. - ContentChild 用于获取通过内容投影方式设置到试图中的元素。
  8. - ViewChild 用于从模板中获取匹配的元素
  9. - 在父组件的 ngAfterContentInit 生命周期中,才能获取通过 ContentChild 查询的元素
  10. - 在父组件的 ngAfterViewInit 生命周期中,才能获取通过 ViewChild 查询的元素。
  11. 相关文章
  12. - [https://blog.angular-university.io/angular-ng-template-ng-container-ngtemplateoutlet/](https://blog.angular-university.io/angular-ng-template-ng-container-ngtemplateoutlet/)
  13. - [https://segmentfault.com/a/1190000008707828](https://segmentfault.com/a/1190000008707828) Angular2 ContentChild & ContentChildren
  14. <a name="sbhI0"></a>
  15. ### ngTemplateOutlet/ngTemplateOutletContext 使用
  16. > [https://codesandbox.io/s/strange-bartik-9vkx8j?file=/src/app/app.component.ts](https://codesandbox.io/s/strange-bartik-9vkx8j?file=/src/app/app.component.ts) 实时尝试
  17. ```typescript
  18. @Component({
  19. selector: '',
  20. template: `
  21. <!--
  22. 这里即实现了动态展示模板,并且 ng-container 仅仅是站位标签,
  23. 实际dom渲染后并不会有ng-container这一层显示。
  24. <ng-container *ngTemplateOutlet="estimateTemplate" *ngTemplateOutletContext="ctx"></ng-container>
  25. -->
  26. <ng-container *ngTemplateOutlet="estimateTemplate; context: ctx"></ng-container>
  27. <!--
  28. 注意,这里的let-xxx,这个xxx是模板中能够使用的变量
  29. estimate 则是 ts 中定义的ctx的属性值。这个对应关系需要注意
  30. -->
  31. <ng-template #estimateTemplate let-lessonsCounter="estimate">
  32. <div> Approximately {{lessonsCounter}} lessons ... </div>
  33. </ng-template>
  34. `})
  35. export class AppoComponent {
  36. total = 10;
  37. ctx = { estimate: this.total }
  38. }
  39. /*
  40. * 或者下面的方式也可以
  41. * 上面是简写,这里是完整写法。
  42. */
  43. @Component({
  44. selector: "app-root",
  45. template: `
  46. <!--
  47. 这里的 $implicit 看着有点怪,不过算是固定写法,
  48. 可以看的到,还可以传入其他参数,如示例的 idx
  49. -->
  50. <ng-container
  51. [ngTemplateOutlet]="estimateTemplate"
  52. [ngTemplateOutletContext]="{ $implicit: estimate, idx: 1 }"
  53. ></ng-container>
  54. <ng-template #estimateTemplate let-estimate let-position="idx">
  55. <div>Approximately {{ estimate }} lessons ...</div>
  56. <div>positoin = {{position}}</div>
  57. </ng-template>
  58. `,
  59. styleUrls: ["./app.component.css"]
  60. })
  61. export class AppComponent {
  62. total = 10;
  63. estimate = 20;
  64. }

参考

@Directive({ selector:’[appChbgcolor]’ }) export class ChangeBgColorDirective { constructor(private el: ElementRef, private renderer: Renderer) { }

// 这里监听了 mouseover 事件 // 当鼠标移动到该 host 组件时,执行其中的逻辑 @HostListener(‘mouseover’) onMouseOver() { this.ChangeBgColor(‘red’) }

@HostListener(‘click’) onClick() { // click event }

@HostListener(‘mouseleave’) onMouseLeave() { // mouseleave event }

ChangeBgColor(color: string) { this.renderer.setElementStyle(this.el.nativeElement, ‘color’, color); } }

// 使用该 directive `

{{title}}

`

  1. 所以该 example 可以看出 **HostListener 指令的作用是处理来自host(托管)元素的事件**。
  2. 再来看一个 HostBinding example
  3. ```typescript
  4. // 这里使用 border 变量绑定了 host 元素的 border 属性
  5. @HostBinding('style.border') border: string;
  6. @HostListener('mouseover') onMouseOver() {
  7. // 这里修改 border 变量的值,来达到修改 host 元素 border 属性的目的
  8. this.border = '5px solid green'
  9. }

所以,可以看出 HostBinding 的作用就是修改 host 元素的属性

参考

到这里我们对 accordion 组件也有了一定的了解了。

朋友你好,如果你对我的文章感兴趣,欢迎关注我的 Github (https://github.com/llccing),或者掘金 (https://juejin.cn/user/3227821867281079),或者语雀 (https://www.yuque.com/uov16w),感谢你的支持!