如何解决如何使用事件监听器更新注入
我正在尝试更新注入中的变量,但仅当我使用间隔时才有效。我想使用一个eventListener。两者都可以调用该函数,但仅在使用间隔时才更改变量。
landing-path.service.ts
import { Injectable,HostListener } from '@angular/core';
//window width
var w = window.innerWidth;
@Injectable({
providedIn: 'root'
})
export class LandingPathService{
newpath1 = '13';
resize;
load;
constructor() {this.resize = window.setInterval(() => this.onResize(),200);/*this.resize = window.addEventListener('resize',this.onResize);*/
}
onResize(){
w = window.innerWidth;
this.newpath1 = `M0,0 l${w},0 0,200 -${w},0 z`;
console.log(this.resize);
}
}
landing.component.ts
import { Component,ViewChild,ElementRef,HostListener } from '@angular/core';
import { SubstringService } from '../substring.service';
import { LandingPathService } from './../landing-path.service';
@Component({
selector: 'app-landing',templateUrl: './landing.component.html',styleUrls: ['./landing.component.css'],})
export class LandingComponent{
constructor(readonly ss: SubstringService,readonly lps: LandingPathService
) {}
ngAfterViewInit(){
let pan = (<HTMLInputElement>document.getElementById('svg1'));
pan.setAttribute('d',`${this.lps.newpath1}`);
}
@HostListener('window:resize',['$event'])
onResize(event) {
let pan = (<HTMLInputElement>document.getElementById('svg1'));
pan.setAttribute('d',`${this.lps.newpath1}`);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。