[英]Change background image button in AngularJS
309/5000 Hello, This's my first post. 309/5000您好,这是我的第一篇文章。 I'm developing a brutal TicTacToe with AngularJS.
我正在用AngularJS开发残酷的TicTacToe。 But I can't change the images of the buttons when I click on it.
但是单击按钮时,无法更改按钮的图像。 The game is not finished yet, I needed help only for this thing.
游戏还没有结束,我只需要帮助。 Sorry for the English "MACCHERONICO" (italian pasta).
抱歉,英语为“ MACCHERONICO”(意大利面)。 Thank you all.
谢谢你们。
app.controller('gameController', function ($scope, $routeParams) {
$scope.p1name = $routeParams.p1name;
$scope.p2name = $routeParams.p2name;
$scope.griglia = ['', '', '', '', '', '', '', '', ''];
$scope.player = $scope.p1name;
$scope.turno = 0;
var img_array = ['imm\broccolo-sorridente-alimenti-verdure-1138234.png',
'imm\pomodoro-sorridente-alimenti-verdure-1130307.png'];
$scope.img;
$scope.riempi = function (a) {
console.log("entrati in riempi casella " + a);
if ($scope.turno == 0) {
$scope.griglia[a - 1] = "x";
} else {
$scope.griglia[a - 1] = "o";
}
$scope.aggiornaimg();
$scope.cambiaturno();
};
$scope.cambiaturno = function () {
console.log("entrati in cambiaturno");
if ($scope.turno == 0) {
$scope.turno = 1;
$scope.player = $scope.p1name;
} else {
$scope.turno = 0;
$scope.player = $scope.p2name;
}
};
$scope.aggiornaimg = function () {
console.log("entrati in aggiornaimg");
for (let index = 0; index < 9; index++) {
if ($scope.griglia[index] == "x") {
console.log("entrati in aggiornaimg primo if");
document.getElementById("i" + index + 1).currentSrc = img_array[0];
} else if ($scope.griglia[index] == "o") {
document.getElementById("i" + index + 1).currentSrc = img_array[1];
}
}
};
My HTML 我的HTML
<table >
<button type="button" ng-click=(riempi(1))>
<img ng-src="{{img}}" id="i1">
</button>
</td>
<td>
<button type="button" ng-click=(riempi(2))>
<img src="" id="i2"> </button>
</td>
....
</table>
I think your problem is that "i" + index + 1
is not resulting in the id you want: 我认为您的问题是
"i" + index + 1
不会导致您想要的ID:
for (let index = 0; index < 9; index++) { console.log("i" + index + 1); }
Welcome to the beauty of JS, what you need is: 欢迎来到JS之美,您需要的是:
for (let index = 0; index < 9; index++) { console.log("i" + (index + 1)); }
This way, the sum is perform before the concatenation. 这样,总和在连接之前执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.