[英]Angular2 trigger directive from component on click
我有一個指令,可以將框陰影添加到頁面上的任何懸停元素上,但是我需要它才能在單擊按鈕后開始應用陰影。 我的問題是,它僅適用於單個元素。
將鼠標懸停后,它僅適用於標題。 我需要將其應用於任何懸停的元素。
我的app.component:
@Component({
moduleId: module.id,
selector: 'my-app',
template: `
<h1 myHighlight="orange">{{title}} {{clickedElement | async}}</h1>
<nav>
<a routerLink="/dashboard" routerLinkActive="active">Dashboard</a>
<a routerLink="/heroes" routerLinkActive="active">Heroes</a>
<a routerLink="/secret-heroes" *ngIf="authService.loggedIn()" routerLinkActive="active">Secret Heroes</a>
<a (click)=authService.login() *ngIf="!authService.loggedIn()">Log In</a>
<a (click)=authService.logout() *ngIf="authService.loggedIn()">Log Out</a>
<a (click)=giveFeedback()>Give Feedback</a>
<a (click)="listening = !listening" >Give Feedback2</a>
<button id="modalButton" type="button" (click)="feedbackModal.show()">test</button>
<my-feedback-modal>
</my-feedback-modal>
</nav>
<router-outlet></router-outlet>
`,
styleUrls: ['app.component.css']
})
export class AppComponent {
//@Input() highlight: boolean = false;
title = 'Tour of Heroes';
@ViewChild(ModalComponent) modal: ModalComponent;
@ViewChild(HighlightDirective) highlightDir: HighlightDirective;
@ViewChild(FeedbackModalComponent) feedbackModal: FeedbackModalComponent;
constructor(private authService: AuthService, private el: ElementRef, private cdr: ChangeDetectorRef) {
this.cdr = cdr;
}
clickedElement:BehaviorSubject<ElementRef> = new BehaviorSubject(this.el);
ngAfterViewInit() {
//this.clickedElement.next(this.highlightDir.getElement().nativeElement.nodeName);
}
ngDoCheck() {
}
giveFeedback(): void {
this.highlightDir.startFeedback();
this.cdr.detectChanges();
//this.highlight = true;
}
}
我的Highlight.directive:
@Directive({
selector: 'a, abbr, address, article, body, br, button, div, h1, h2, h3, h4, h5, h6, header, hr, i, iframe, img, ' +
'input, label, li, link, meta, nav, object, ol, option, output, p, param, pre, section, select, small, source, span,' +
'summary, table, tbody, td, textarea, tfoot, th, thead, time, title, tr, u, ul, video'
})
export class HighlightDirective {
elementsArray: string[];
listening: boolean = false;
constructor(private el: ElementRef, private cdr: ChangeDetectorRef) {
this.cdr = cdr;
this.elementsArray = ["a", 'abbr', 'address', 'article', 'body', 'br', 'button', 'div', 'h1', 'h2', 'h3', 'h4', 'h5'
, 'h6', 'header', 'hr', 'i', 'iframe', 'img', 'input', 'label', 'li', 'link', 'meta', 'nav', 'object', 'ol', 'option'
, 'output', 'p', 'param', 'pre', 'section', 'select', 'small', 'source', 'span', 'summary', 'table', 'tbody', 'td'
, 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'u', 'ul', 'video'];
}
//@Input() defaultColor: string;
//@Input() listening: boolean = false;
//check: boolean = false;
public getElement(): ElementRef {
return this.el;
}
public startFeedback(): boolean {
this.listening = true;
this.cdr.detectChanges();
return true;
}
@HostListener('click') onClick() {
if(this.listening) {
document.getElementById('modalButton').click();
this.listening = false;
}
}
@HostListener('mouseenter') onMouseEnter() {
if(this.listening) {
this.el.nativeElement.style.boxShadow = '0 0 0 5px yellow';
this.el.nativeElement.parentNode.style.boxShadow = null;
}
}
@HostListener('mouseleave') onMouseLeave() {
if(this.listening) {
this.el.nativeElement.style.boxShadow = null;
this.el.nativeElement.parentNode.style.boxShadow = '0 0 0 5px yellow';
let check = false;
for (let entry of this.elementsArray) {
if (this.el.nativeElement.parentNode.nodeName == entry.toUpperCase()) {
check = true;
break;
}
}
if (!check)
this.el.nativeElement.parentNode.style.boxShadow = null;
}
}
}
任何幫助將不勝感激。
問題是您使用@ViewChild
而不是@ViewChildren
。 使用ViewChild時,它僅處理可以在模板中找到的HighlightDirective
的第一個實例。
除了您在此處做出的其他一些晦澀的選擇外,我還想說您必須更改為以下內容:
@ViewChildren(HighlightDirective) highlightDirs: QueryList<HighlightDirective>;
然后,您必須將GiveFeedback函數更改為此:
giveFeedback(): void {
this.highlightDirs.forEach((highlightDir: HightlightDirective) => {
highlightDir.startFeedback();
});
}
您的任何代碼內都不需要changeDetectionRef。 僅當在組件/指令上放置changeDetection: ChangeDetectionStrategy.OnPush
,才有必要
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.