[英]Angular 2 : How can I call function from directive to component?
Angular web application, added 'screen full' package and used in header worked fine. Angular web 应用程序,添加了“全屏” package 并用于 header 工作正常。 But when I used to particular page form it will show the error.但是当我习惯于特定的页面形式时,它会显示错误。 Cant we used particular page?我们不能使用特定页面吗?
enter image description here在此处输入图像描述
Directive指示
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 .html
<a href="javascript:void(0)" class="text-dark" (click)="click()">
<app-feather-icons [icon]="'maximize'"></app-feather-icons>
</a>
component成分
import { ToggleFullscreenDirective } from '../../../shared/directives/fullscreen.directive';
export class ReportComponent implements OnInit {
@ViewChild(ToggleFullscreenDirective) appToggleFullscreen = null;
constructor() {}
click() {
this.appToggleFullscreen.onClick();
}
}
I got error "core.js:6150 ERROR TypeError: Cannot read property 'onClick' of undefined"我收到错误“core.js:6150 ERROR TypeError: Cannot read property 'onClick' of undefined”
this.appToggleFullscreen
is undefined, because you're not calling it in html. this.appToggleFullscreen
未定义,因为您没有在 html 中调用它。
click() {
this.appToggleFullscreen.onClick();
}
You have to do it like this,你必须这样做,
<a href="javascript:void(0)" class="text-dark" (click)="click()" appToggleFullscreen>
<span>component here</span>
</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.