[英]Angular 2 : How can I call function from directive to component?
Angular web 应用程序,添加了“全屏” package 并用于 header 工作正常。 但是当我习惯于特定的页面形式时,它会显示错误。 我们不能使用特定页面吗?
指示
import {Directive, HostListener} from '@angular/core';
declare var require: any;
const screenFull = require('screenfull');
@Directive({
selector: '[appToggleFullscreen]'
})
export class ToggleFullscreenDirective {
@HostListener('click') onClick() {debugger
if (screenFull.isEnabled) {
screenFull.toggle();
}
}
}
.html
<a href="javascript:void(0)" class="text-dark" (click)="click()">
<app-feather-icons [icon]="'maximize'"></app-feather-icons>
</a>
成分
import { ToggleFullscreenDirective } from '../../../shared/directives/fullscreen.directive';
export class ReportComponent implements OnInit {
@ViewChild(ToggleFullscreenDirective) appToggleFullscreen = null;
constructor() {}
click() {
this.appToggleFullscreen.onClick();
}
}
我收到错误“core.js:6150 ERROR TypeError: Cannot read property 'onClick' of undefined”
this.appToggleFullscreen
未定义,因为您没有在 html 中调用它。
click() {
this.appToggleFullscreen.onClick();
}
你必须这样做,
<a href="javascript:void(0)" class="text-dark" (click)="click()" appToggleFullscreen>
<span>component here</span>
</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.