Is there a way to dynamically bind the src attribute of an img tag in Angular by calling a function from Typescript? Here's the HTML code snippet:
<button (click)="createBoard()"> Create Board</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>
And this is the corresponding Typescript code:
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] ++;
}
}
The issue reported here is that the images are not being loaded. Is there a workaround to fetch image sources using a function in Angular or Typescript?