【问题标题】:Angular 6- Dynamically change css properties for *ngFor elements on variable change of same componentAngular 6-在同一组件的可变更改上动态更改 *ngFor 元素的 css 属性
【发布时间】:2019-01-09 01:22:16
【问题描述】:

请看下图。我在画布上有 p 标签列表。在画布下方,我有多种颜色和字体大小。

下面是我的场景

  1. 在画布上添加文本。我想添加多少就添加多少。
  2. 选择任何文本并更改所选 p 标签的颜色和字体大小。

目前我做了以下代码:

1. HTML

<ion-row #canvasRow id="canvasRow">
    <ion-col *ngFor="let textarea of textAreasList; let textarea_index= index">
        <p absolute-drag style="border: 1px dotted black;
            height: 40px;
            width: 60%;
            z-index: 10000;" (click)="changeTextStyle($event)" (txtCChange)="changeTxtColor($event)"
            (txtSChange)="changeTxtSize($event)">{{textarea}}</p>
            <button (click)="removeTextArea(textarea_index)">Remove</button>
    </ion-col>
  </ion-row>

2。颜色代码选择

<ion-row id="top-toolbar">
    <ion-col>
        <ion-scroll scrollX="true">
            <ion-buttons id="ionBtnGroup">
                <button *ngFor="let colour of availableColours" icon-only ion-button (click)="changeColour(colour)">
                    <ion-icon [style.color]="colour" name="brush"></ion-icon>
                </button>
            </ion-buttons>
        </ion-scroll>
    </ion-col>
  </ion-row>

3。字体更改代码

<ion-buttons right *ngIf="!isDraw && !isRotate" >
        <button color="dark" ion-button icon-only (click)="changeFontSize(10)"><div class="galeria"><span>10px</span></div></button>
        <button color="dark" ion-button icon-only (click)="changeFontSize(15)"><div class="galeria"><span>15px</span></div></button>
        <button color="dark" ion-button icon-only (click)="changeFontSize(20)"><div class="galeria"><span>20px</span></div></button>
        <button color="dark" ion-button icon-only (click)="changeFontSize(30)"><div class="galeria"><span>30px</span></div></button>
        <button color="dark" ion-button icon-only (click)="changeFontSize(50)"><div class="galeria"><span>50px</span></div></button>
    </ion-buttons>

4.字体更改功能

changeFontSize(size){
  this.selectedFontSize = size+'px';
  this.txtSChange.emit({size: size+'px'});
}

5.变色功能

 changeColour(colour){
    if(this.isDraw){
      this.currentColour = colour;
    }else{
      this.selectedColor = colour;
      this.txtCChange.emit({color: colour});
    }
}

6.颜色和字体大小适用代码

@Output() txtCChange = new EventEmitter();
@Output() txtSChange = new EventEmitter();

changeTextStyle(event: Event){
    let element = event.target as HTMLInputElement;
    element.style.color = this.selectedColor;
    element.style.fontSize = this.selectedFontSize;
  }

  changeTxtColor(event){
    this.selectedColor = event.color;
    this.changeTextStyle(event);
  }

  changeTxtSize(event){
    this.selectedFontSize = event.size;
    this.changeTextStyle(event);
  }

如果有任何困惑,请告诉我。上面的代码不起作用。我想知道更有效的方法。

【问题讨论】:

    标签: javascript html css angular6 angular-observable


    【解决方案1】:

    我建议在*ngFor 循环中使用ngStyle 来处理所有用于样式的DOM 操作。在 Angular 中直接操作 DOM 元素被认为是不可行的。这可以让您摆脱大量的操作代码。

    textAreasList 中的每个元素都应该是一个包含所有必要属性(如实际文本内容)和样式属性(如位置、颜色、大小等)的对象。示例:{ content: "Ravi1", style: {height: 40, width: 60, color: "#ff0000"} }

    根据用户是先选择颜色然后在第二次单击中选择文本还是反过来,您可以将选定的颜色(或选定的文本元素)存储在控制器中以引用它,一旦文本是clicked(颜色被点击)。

    模板:

    <button *ngFor="let colour of availableColours" icon-only ion-button (click)="selectedColour = colour)">
       <ion-icon [style.color]="colour" name="brush"></ion-icon>
    </button>
    
    
    <p  [...] (click)="applyStyles(textarea)">{{textarea.content}}</p>
    

    控制器:

    applyStyles(textarea){
      textarea.styles.colour = this.selectedColor;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-17
      • 2021-12-25
      • 2020-06-22
      • 2019-03-14
      • 2019-05-28
      • 2014-03-28
      • 2011-05-03
      • 2019-02-23
      相关资源
      最近更新 更多