admin管理员组文章数量:1356285
I have a dropdown with a template like this:
<div class="dropdown-trigger" (click)="contentToggle()">
<ng-content select="dropdown-trigger"></ng-content>
</div>
<div class="dropdown-content" *ngIf="showContent">
<ng-content select="dropdown-content"></ng-content>
</div>
I'd like to be able to use contentToggle()
in whatever I put in the ng-content
so that I can have additional elements that can be used to close the dropdown, for example I might want a close button... What's the best way to do this?
I have a dropdown with a template like this:
<div class="dropdown-trigger" (click)="contentToggle()">
<ng-content select="dropdown-trigger"></ng-content>
</div>
<div class="dropdown-content" *ngIf="showContent">
<ng-content select="dropdown-content"></ng-content>
</div>
I'd like to be able to use contentToggle()
in whatever I put in the ng-content
so that I can have additional elements that can be used to close the dropdown, for example I might want a close button... What's the best way to do this?
- what is that you are trying to do. explain as it is unclear – Aravind Commented Apr 5, 2017 at 16:34
- @Aravind added more detail, is that enough? – nick Commented Apr 5, 2017 at 16:35
- this is handled by bootstrap by default. – Aravind Commented Apr 5, 2017 at 16:37
- @Aravind I'm not using bootstrap – nick Commented Apr 5, 2017 at 16:37
- create a plunker I will fix – Aravind Commented Apr 5, 2017 at 16:38
2 Answers
Reset to default 8This will do the trick:
<dropdown #dropdown>
<button dropdownTrigger (click)="dropdown.toggleDropdown()">Click me</button>
</dropdown>
You just assign a local template variable to the ponent which gives you access to everything the ponent has in it. Including the function you want to call.
Note that you should/need to also change the select
bits to this:
<ng-content select="[dropdownTrigger]"></ng-content>
<ng-content select="[dropdownContent]"></ng-content>
Angular allow you to do this trick, Example:
import { Component } from '@angular/core'
@Component(){...}
export class DropdownComponent {
toggleDropdown() {...}
}
//parent.ponent.html
<dropdown-content #myDropdown>
<a (click)="myDropdown.toggleDropdown()">Close the dropdown</a>
</dropdown-content>
If you want to get a callback to the event I remend you to read the Output Decorator
本文标签: javascriptAngular 2 make function available in ngcontentStack Overflow
版权声明:本文标题:javascript - Angular 2 make function available in ng-content - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744028068a2578401.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论