我正在制作一个有50个按钮的游戏,每个按钮都有一个颜色和id的身份。单击每个按钮时,颜色都会更改为已定义的颜色。如果用户点击第25个按钮,他就赢了。只有三次点击尝试。然而,我无法完成创建按钮的第一步。请帮帮忙。我使用angularjs1。
<html ng-app="Bluebox">
<head>
<title>BlueBox</title>
<script src="angular.js"></script>
</head>
<body>
<div ng-controller="BoxController">
<button type="button" value = "bluebox" >
<li ng-repeat="x in boxlength">
{{index+1}}
</li>
</button>
</div>
<script>
angular.module("Bluebox",[])
.controller("BoxController",["$scope",function($scope){
$scope.boxlength=50;
$scope.index=0;
}])
</script>
</body>
</html>
发布于 2018-03-26 21:57:14
您的ng-repeat不在正确的位置。你想重复按钮。
<button ng-repeat="..." type="button" value="bluebox"></button>
那么ng-repeat的格式不正确。这个指令不会重复多次,它会在一个数组中重复。
我们通常要做的是创建一个基于数字创建数组的方法:
<button ng-repeat="i in times(number)" type="button" value="bluebox"></button>
Get number可能类似于:
$scope.number = 5;
$scope.times= function(x) {
return new Array(num);
}
由于创建的数组具有千变万化的“值”,因此需要使用$index进行跟踪
function Main($scope) {
$scope.number = 50;
$scope.times= function(x) {
return new Array(x);
}
}
angular.module('test', []);
angular.module('test')
.controller('Main', Main);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script>
<div ng-app="test">
<div ng-controller="Main">
<button ng-repeat="i in times(number) track by $index" type="button">{{$index}}</button>
</div>
</div>
https://stackoverflow.com/questions/49490620
复制相似问题