我有两个不同的html文件,
test1.html test2.html test2.component.ts
1) test1.html:
<div style="width:100%;height:20%"></div>2) test2.html:
如果单击rectangle function,test1.html边框半径应更改为0px。
如果我点击roundCorder函数,test1.html边框半径应该会变成10px;
<img (click)="rectangle()" [src]="imgSrc" (mouseover)="rectangleHover()" (mouseout)="rectangleOut()" style="height:100px;float:left;margin:15px;" >
<img (click)="roundCorner()" [src]="imgSrc1" (mouseover)="roundHover()" (mouseout)="roundOut()" style="height:100px;float:left;margin:15px;">
3) test2.components.ts:
roundCorner(){
// want to change test1.html file div border radius as 10px;
}发布于 2018-08-02 03:22:26
test1.html
<div #elem1 style="width:100%;height:20%"></div>test2.html
<img #elem2 (click)="rectangle()" [src]="imgSrc" (mouseover)="rectangleHover()" (mouseout)="rectangleOut()" style="height:100px;float:left;margin:15px;" >
<img (click)="roundCorner()" [src]="imgSrc1" (mouseover)="roundHover()" (mouseout)="roundOut()" style="height:100px;float:left;margin:15px;"> test2.component.ts
import {ElementRef,Renderer2} from '@angular/core';
@ViewChild('elem2') el:ElementRef;
constructor(private rd: Renderer2,sharedService:SharedService) {
this.el=this.sharedService.get();
}
roundCorner(){
// want to change test1.html file div border radius as 10px;
this.el.style.border-radius='10px';
}
rectangle(){
this.el.style.border-radius='0px';
}shared.service.ts
@Injectable()
class SharedService{
let elem1:any
set(element:any)
{
this.elem1=element;
}
get(){
return this.elem1;
}test1.component.ts
@ViewChild('elem1'):ElementRef
constructor(sharedService:SharedService,elementRef:ElementRef){
}
ngOnInit()
{
this.sharedService(this.elem1);
}您必须使用一个共享服务来访问两个组件之间的dom元素引用变量,然后分别设置边框半径的属性。
在component.ts文件和app.module.ts中导入{ SharedService },并将SharedService放在app.module.ts中的提供程序数组下
https://stackoverflow.com/questions/51640029
复制相似问题