英文:
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.
<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 here is my 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] ++;
}
}
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
<img src="getImage(a,b)" (click)="changeImg(a,b, $event)">
to dynamically interpolate it use the property binding instead
<img [src]="getImage(a, b)" (click)="changeImg(a, b, $event)">
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<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'};
}
}
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='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'}
}
}
So you use
<img src="board[a][b].path"
(click)="changeImg(a,b, $event)">
And not force Angular to execute the function getImage
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论