Change color of popup (angular, TS) 改变弹出框的颜色 (Angular, TypeScript)

huangapple go评论101阅读模式
英文:

Change color of popup (angular, TS)

问题

这是您要翻译的内容:

"I build a custom popup which is visible when I click a button. When I click somewhere else on the document the popup should be closed / invisible. That works pretty well. Now I want to change the style property of this popup. The problem is that i cant change it. The code below returns that the HTML object is null but if i click another buttom with same functionality the style changes.

Thats my code so far

tooltip.component.ts

  1. popup = false;
  2. // open popup
  3. openToolTip($event: {
  4. target: any; stopPropagation: () => void;
  5. })
  6. {
  7. $event.stopPropagation();
  8. this.popup = !this.popup;
  9. testvariable = document.getElementByID("popupId");
  10. testvariable.style.backgroundcolor = "green"; //backgroundcolor just for testing
  11. }
  12. }
  13. // close popup if clicked on document
  14. @HostListener('document:click', ['$event']) onDocumentClick(event: any) {
  15. this.popup = false;
  16. }
  17. constructor() { }
  18. ngOnInit(): void {
  19. }
  20. } ```
  21. html
  22. ``` <span class="info-icon" (click)="openToolTip($event)">
  23. <mat-icon>info_outline</mat-icon>
  24. </span>
  25. <div *ngIf="popup" id="popupId" class="popup" (click)="$event.stopPropagation()">
  26. <div class="text-box">
  27. </div>
  28. <!-- close-button -->
  29. <a class="close" (click)="popup = false">&times;</a>
  30. </div> ```
  31. EDIT:
  32. I used the timeout function like Elikill58 said. Its a workaround but it solves my problem for now :)"
  33. 请注意,我已经删除了代码部分并进行了翻译。如果您有任何其他需要,请随时告诉我。
  34. <details>
  35. <summary>英文:</summary>
  36. I build a custom popup which is visible when I click a button. When I click somewhere else on the document the popup should be closed / invisible.
  37. That works pretty well.
  38. Now I want to change the style property of this popup. The problem is that i cant change it.
  39. The code below returns that the HTML object is null but if i click another buttom with same functionality the style changes.
  40. Thats my code so far
  41. tooltip.component.ts

export class TooltipComponent implements OnInit {
popup = false;

// open popup
openToolTip($event: {
target: any; stopPropagation: () => void;
})
{

  1. $event.stopPropagation();
  2. this.popup = !this.popup;

testvariable = document.getElementByID("popupId");
testvariable.style.backgroundcolor = "green"; //backgroundcolor just for testing

}
}

// close popup if clicked on document
@HostListener('document:click', ['$event']) onDocumentClick(event: any) {
this.popup = false;
}

constructor() { }

ngOnInit(): void {
}

}

  1. html

<span class="info-icon" (click)="openToolTip($event)">
<mat-icon>info_outline</mat-icon>
</span>

<div *ngIf="popup" id="popupId" class="popup" (click)="$event.stopPropagation()">

  1. &lt;div class=&quot;text-box&quot;&gt;
  2. &lt;/div&gt;
  3. &lt;!-- close-button --&gt;
  4. &lt;a class=&quot;close&quot; (click)=&quot;popup = false&quot;&gt;&amp;times;&lt;/a&gt;

</div>```

EDIT:
I used the timeout function like Elikill58 said. Its a workaround but it solves my problem for now Change color of popup (angular, TS)
改变弹出框的颜色 (Angular, TypeScript)

答案1

得分: 0

以下是翻译好的部分:

  1. 等待它

你可以使用超时函数:

  1. timeout(function() {
  2. var testvariable = document.getElementByID("popupId");
  3. testvariable.style.backgroundcolor = "green";
  4. }, 0);
  1. 使用 ngStyle 设置样式

如果样式应该依赖于值,你可以这样做:

  1. <div [ngStyle]="{'background-color': something ? 'green' : 'red'}">
  2. </div>
  1. 更改默认样式

这将更改所有弹出窗口的样式,无需进行JS操作:

  1. .popup {
  2. background-color: green;
  3. }
  1. 使用 ID 更改样式

如果你使用特定的ID,你可以这样做:

  1. #popupId {
  2. background-color: green;
  3. }

所有这些方法都有优点和缺点,你应该选择与你所寻找的相对应的方法。

英文:

The problem comes from the element isn't known yet. You are checking it too fast. There is multiple way to fix it:

  1. Wait for it.

You can use the timeout function:

  1. timeout(function() {
  2. var testvariable = document.getElementByID(&quot;popupId&quot;);
  3. testvariable.style.backgroundcolor = &quot;green&quot;;
  4. }, 0);
  1. Set style with ngStyle.

If the style should depend of values, you can do like:

  1. &lt;div [ngStyle]=&quot;{&#39;background-color&#39;: something ? &#39;green&#39; : &#39;red&#39;}&quot;&gt;
  2. &lt;/div&gt;
  1. Change style by default.

This will change the style for all popup, without requiring JS manipulation:

  1. .popup {
  2. background-color: green;
  3. }
  1. Change style with ID.

If you are using specific ID, you can do like:

  1. #popupId {
  2. background-color: green;
  3. }

All ways have advantages and disadvantages, you should take the one that correspond to what you are looking for.

huangapple
  • 本文由 发表于 2023年2月16日 16:27:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定