gpt4 book ai didi

javascript - 单击时一起处理迭代中的所有项目 - angularjs

转载 作者:行者123 更新时间:2023-12-03 09:52:27 25 4
gpt4 key购买 nike

我有一个 Controller ,在 Controller 中我正在迭代
ng-click="minusOne($index)"ng-click="plusOne($index)" 但如果我点击第一个 block ,它会在所有迭代,但我想如果我单击第一个 block ,它应该仅在第一个 block 上工作

      .controller('MainController', ['$scope', '$rootScope', function($scope, $rootScope) {

$scope.fine = [{
likes: 0,
dislikes: 0
}];
$scope.plusOne = function(index) {
$scope.fine[index].likes += 1 * 10;
};
$scope.minusOne = function(index) {
$scope.fine[index].dislikes += 1 * 10;
};
$scope.buildBreak = function(index) {
$scope.fine[index].likes += 1 * 50;
};

}])

HTML

<div class="item item-body addfineinfo" ng-repeat="putfine in fine">

<a ng-click="plusOne($index)" class="button button-small button-dark">+ Fine</a>
<a ng-click="minusOne($index)" class="button button-small button-dark">- Fine</a>
<a ng-click="buildBreak($index)" class="button button-small button-assertive">BUILD BREAK</a>
<input name="message" readonly type="text" value="{{ putfine.likes - putfine.dislikes }}" placeholder="Suhr">

值(value)应该出现在这个字段中,这也应该出现在交互中

最佳答案

html

        <!DOCTYPE html>
<html>

<head>
<script data-require="angular.js@1.4.0" data-semver="1.4.0" src="https://code.angularjs.org/1.4.0/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>

<body>
<div ng-app="myApp" ng-controller="ctrl">


<div ng-repeat="developerAdd in developer">
{{ developerAdd.likes}} - {{developerAdd.dislikes }}
<br/>
<a ng-click="plusOne($index)" class="button button-small button-dark">+ Fine</a>
<a ng-click="minusOne($index)" class="button button-small button-dark">- Fine</a>
<a ng-click="buildBreak($index)" class="button button-small button-assertive">BUILD BREAK</a>

</div>
</div>
</body>

</html>

Controller

// Code goes here

var app = angular.module('myApp', []);

app.controller('ctrl', function($scope) {

$scope.developer = [{
id: 1
},{
id: 2
},{
id: 3
},{
id: 4
}];

$scope.plusOne = function(index) {
if ($scope.developer[index].likes == undefined) $scope.developer[index].likes = 0;
$scope.developer[index].likes += 1 * 10;
};

$scope.minusOne = function(index) {
if ($scope.developer[index].dislikes == undefined) $scope.developer[index].dislikes = 0;
$scope.developer[index].dislikes += 1 * 10;
};

$scope.buildBreak = function(index) {
if ($scope.developer[index].likes == undefined) $scope.developer[index].likes = 0;
$scope.developer[index].likes += 1 * 50;
};
});

关于javascript - 单击时一起处理迭代中的所有项目 - angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30851174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com