我需要獲取在給定 ng-repeat 上選擇的先前值,以便我可以在業務需求的情況下將其回傳。我怎樣才能做到這一點?
我有一個 ng-click,但在事件點擊時我已經選擇了新專案,但如果它存在,我也需要舊專案。
PS:我是 angular js 的新手,因此請原諒我的菜鳥問題!
謝謝。
uj5u.com熱心網友回復:
您可以保留一系列選擇“歷史”
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/404684.html
標籤:
