我有5个带有几个ID的按钮。 ID是随机名称。我试图将此ID名称推送到名为myClicks的数组中。什么是最简单的解决方案。我尝试推入“data.id”和“id”但没有工作;该数组仍为空。
<!DOCTYPE html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
function MyCtrl($scope) {
$scope.foo = "I'm foo!";
$scope.lines = [];
$scope.myClicks=[];
id=$scope.id;
$scope.clickme = function(){
//alert("test");
$scope.lines.push($scope.lines.length);
$scope.myClicks.push({id});
console.log('user'+ 'path was as followed');
}
}
</script>
</head>
<body>
<div ng-app ng-controller="MyCtrl">
<button id="uno" ng-click="clickme()"ng-model="data.id">Path A</button><br>
<button id="dos" ng-click="clickme()" ng-model="data.id">Path B</button><br>
<button is="cuatro" ng-click="clickme()" ng-model="data.id">Path C</button><br>
<button id="cinco" ng-click="clickme()" ng-model="data.id">Path D</button><br>
<button id="sieta" ng-click="clickme()" ng-model="data.id" value="dadsa">Path E</button>
<div ng-repeat="line in lines">
UserPath:{{myClicks}}
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
function MyCtrl($scope) {
$scope.lines_dic = {}
$scope.clickme = function(val){
if(val in $scope.lines_dic){
alert('Already Present')
return
}
$scope.lines_dic[val] = 1;
}
$scope.values = function(){
return Object.keys($scope.lines_dic);
}
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body>
<div ng-app ng-controller="MyCtrl">
<button id="uno" ng-click="clickme('uno')" ng-model="data.id">Path A</button><br>
<button id="dos" ng-click="clickme('dos')" ng-model="data.id">Path B</button><br>
<button is="cuatro" ng-click="clickme('cuatro')" ng-model="data.id">Path C</button><br>
<button id="cinco" ng-click="clickme('cinco')" ng-model="data.id">Path D</button><br>
<button id="sieta" ng-click="clickme('sieta')" ng-model="data.id" value="dadsa">Path E</button>
<div>
UserPath:{{values()}}
</div>
</div>
</body>
&#13;