我正在尝试学习 Angular 的基础知识,但有一个特殊的疑问。我有一组动态生成的单选按钮,默认情况下将选择其中一个。
我想检查并检测单选按钮选择的变化。这可能与 Angular 或基于逻辑的问题并不特别相关。以下是我的尝试。
$scope.selectedReason= function(radio1)
{
$scope.radio7=radio1.id; // now radio7 has the value of radio7 e.g "12345"
$scope.bankAccount=radio1;
}
// another function where I am checking if the selected value is not same as new value
$scope.selectedAmount= function(radioSelect)
{
console.log($scope.radio7); // old value 12345
$scope.preselectedAccount = $scope.radio7; // hoping to store old value
$scope.selectedValue = $scope.preselectedAccount;
if ($scope.selectedValue == $scope.radio7){
console.log("Success");
}
}
基本上,我试图将旧值存储到变量中并进行检查,但很明显,每次出现新值时,变量都会采用该值,因此这样做没有意义。我可以通过任何方式将旧值检查与新值联系起来。
编辑: 输入
<input type="radio" id="child_{{$index}}" name="radio2" ng-model="selectedBankAccount" ng-value="bankAccount" ng-click="selectedReason(bankAccount)">
最佳答案
您可以使用change
事件来实现
// you can pass anything in the function
<input type="radio" (change)="handleChange(child_{{$index}})" />
并在您的 TS 文件中实现 handleChange
函数并执行您想要的操作。
handleChange(id) {
console.log(id);
}
关于javascript - 检查单选按钮选择是否已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61335860/