AngularJS - 在 ng-click on a ng-repeat 之后获取之前选择的选项
AngularJS - get previous selected option after ng-click on a ng-repeat
我需要获取在给定的 ng-repeat 上选择的先前值,以便我可以 return 在业务需求的情况下取回它。
我怎样才能做到这一点?
我有一个 ng-click,但在事件点击时我已经选择了新项目,但我也需要旧项目(如果它存在的话)。
PS:我是 angular js 的新手,因此请原谅我的菜鸟问题!
谢谢。
您可以保留一系列选择 'history'
angular.module('myApp', [])
.controller('myCtrl', ['$scope', function($scope) {
let selHistory = []
$scope.selectThing = "val2"
$scope.recVal = function() {
if (selHistory.length === 0 || selHistory[selHistory.length - 1] !== $scope.selectThing)
selHistory.push($scope.selectThing)
console.log("The history of selections: ", selHistory);
}
$scope.recVal(); // push our intiial variable in there
$scope.revert = function() {
if (selHistory.length > 0) {
selHistory.pop();
$scope.selectThing = selHistory[selHistory.length - 1]
console.log("The history of selections: ", selHistory);
}
}
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-change="recVal()" ng-model="selectThing">
<option value='val1'>1</option>
<option value='val2'>2</option>
<option value='val3'>3</option>
</select>
<button ng-click='revert()'>Revert</button>
</div>
我需要获取在给定的 ng-repeat 上选择的先前值,以便我可以 return 在业务需求的情况下取回它。 我怎样才能做到这一点?
我有一个 ng-click,但在事件点击时我已经选择了新项目,但我也需要旧项目(如果它存在的话)。
PS:我是 angular js 的新手,因此请原谅我的菜鸟问题!
谢谢。
您可以保留一系列选择 'history'
angular.module('myApp', [])
.controller('myCtrl', ['$scope', function($scope) {
let selHistory = []
$scope.selectThing = "val2"
$scope.recVal = function() {
if (selHistory.length === 0 || selHistory[selHistory.length - 1] !== $scope.selectThing)
selHistory.push($scope.selectThing)
console.log("The history of selections: ", selHistory);
}
$scope.recVal(); // push our intiial variable in there
$scope.revert = function() {
if (selHistory.length > 0) {
selHistory.pop();
$scope.selectThing = selHistory[selHistory.length - 1]
console.log("The history of selections: ", selHistory);
}
}
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<select ng-change="recVal()" ng-model="selectThing">
<option value='val1'>1</option>
<option value='val2'>2</option>
<option value='val3'>3</option>
</select>
<button ng-click='revert()'>Revert</button>
</div>