从Typescript函数绑定Angular的img src

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

Angular bind img src from typescript function

问题

我想从TypeScript函数中绑定HTML标记的图像源。以下是我的HTML。

<button (click)="createBoard()">创建棋盘</button>
<table>
  <tr *ngFor="let i of board; let a = index">
    <td *ngFor="let j of i; let b = index">
      <div class="bDefault">
        <img [src]="getImage(a, b)" (click)="changeImg(a, b, $event)">
      </div>
    </td>
  </tr>
</table>

这是我的TypeScript。

size: number = 5;
board: number[][] = [];

createBoard() {
  this.board = new Array(this.size);
  for (let i = 0; i < this.size; i++) {
    this.board[i] = new Array(this.size);
    for (let j = 0; j < this.size; j++) {
      this.board[i][j] = 0;
    }
  }
}

getImage(i: any, j: any) {
  if (this.board[i][j] == 0) {
    return "../../../assets/oth/default.jpg";
  } else if (this.board[i][j] == 1) {
    return "../../../assets/oth/bidak_hitam.jpg";
  } else {
    return "../../../assets/oth/bidak_putih.jpg";
  }
}

changeImg(i: any, j: any, event: Event) {
  if (this.board[i][j] == 2) {
    this.board[i][j] = 0;
  } else {
    this.board[i][j]++;
  }
}

在这段代码中,图像无法找到。是否有一种从TypeScript或Angular函数获取图像的方法?

英文:

I want to bind src of html tag img from typescript function.
Here is my Html.

&lt;button (click)=&quot;createBoard()&quot;&gt; Create Board&lt;/button&gt;
&lt;table&gt;
 &lt;tr *ngFor=&quot;let i of board; let a = index&quot;&gt;
  &lt;td *ngFor=&quot;let j of i; let b = index&quot;&gt;
    &lt;div class=&quot;bDefault&quot;&gt;
      &lt;img src=&quot;getImage(a,b)&quot;  
      (click)=&quot;changeImg(a,b, $event)&quot;&gt;
    &lt;/div&gt;
  &lt;/td&gt;
 &lt;/tr&gt;
&lt;/table&gt; 

And here is my typescript.

size : number = 5;
board : number[][] = [];

createBoard(){
 this.board = new Array(this.size);
 for(let i=0; i&lt;this.size; i++){
  this.board[i] = new Array(this.size);
  for(let j=0; j&lt;this.size; j++){
    this.board[i][j] = 0;
  }
 }
}

getImage(i:any, j:any){
  if(this.board[i][j] == 0){
    return &quot;../../../assets/oth/default.jpg&quot;;
  } else if(this.board[i][j] == 1){
    return &quot;../../../assets/oth/bidak_hitam.jpg&quot;;
  } else {
    return &quot;../../../assets/oth/bidak_putih.jpg&quot;;
  }
}

changeImg(i:any, j:any, event:Event){
  if(this.board[i][j] == 2){
    this.board[i][j]  = 0;
  } else {
    this.board[i][j] ++;
  }
}

In this code the image is not found. Is there a way to get the image from a function in typescript or angular?

答案1

得分: 3

getImage(a,b) 在你的代码中被用作一个静态字符串

<img src="getImage(a,b)" (click)="changeImg(a,b, $event)">

为了动态插值它,请改用属性绑定

<img [src]="getImage(a, b)" (click)="changeImg(a, b, $event)">

这里的 src 会从 getImage 获取动态数据。

英文:

In your code the getImage(a,b) is being used as a static string

&lt;img src=&quot;getImage(a,b)&quot; (click)=&quot;changeImg(a,b, $event)&quot;&gt;

to dynamically interpolate it use the property binding instead

&lt;img [src]=&quot;getImage(a, b)&quot; (click)=&quot;changeImg(a, b, $event)&quot;&gt;

Here the src would get dynamic data from getImage.

答案2

得分: 0

你应该避免在.html中使用函数,我建议您创建一个包含值和路径的对象数组,类似于以下代码:

this.board = new Array(this.size);
for(let i=0; i<this.size; i++){
  this.board[i] = new Array(this.size);
  for(let j=0; j<this.size; j++){
    this.board[i][j] = {value:0, path:'assets/oth/default.jpg'};
  }
}

当更改图像时,您的函数应该像这样:

changeImg(i:any, j:any, event:Event){
  if(this.board[i][j].value == 2){
    this.board[i][j] = {value:0, path:'assets/oth/default.jpg'};
  } else {
    if (this.board[i][j].value == 0)
      this.board[i][j] = {value:1, path:'assets/oth/bidak_hitam.jpg'};
    else
      this.board[i][j] = {value:2, path:'assets/oth/bidak_putih.jpg'};
  }
}

因此,您应该使用以下方式:

<img src="board[a][b].path" (click)="changeImg(a, b, $event)">

不要强制Angular执行getImage函数。

英文:

You should avoid use a function in the .html, I suggest you create an array of object with value and path. Some like

this.board = new Array(this.size);
 for(let i=0; i&lt;this.size; i++){
  this.board[i] = new Array(this.size);
  for(let j=0; j&lt;this.size; j++){
    this.board[i][j] = {value:0,path:&#39;assets/oth/default.jpg&#39;};
  }
 }

When change the images your function

changeImg(i:any, j:any, event:Event){
  if(this.board[i][j].value == 2){
    this.board[i][j]={value:0
                      path=&#39;assets/oth/default.jpg&#39;}
  } else {
    if (this.board[i][j].value==0)
       this.board[i][j]={value:1
                         path=&#39;assets/oth/bidak_hitam.jpg&#39;}
    else
       this.board[i][j]={value:2
                         path=&#39;assets/oth/bidak_putih.jpg&#39;}
  }
}

So you use

  &lt;img src=&quot;board[a][b].path&quot;  
      (click)=&quot;changeImg(a,b, $event)&quot;&gt;

And not force Angular to execute the function getImage

huangapple
  • 本文由 发表于 2023年6月8日 13:50:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428927.html
匿名

发表评论

匿名网友

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

确定