需要获取单选按钮的值并使用HTML和angularJS将其传递给后端,并且前端中显示的数据是一个列表

问题描述 投票:0回答:2

//这是我的HTML代码,其中从后端返回一个列表。

<ul> <li ng-repeat=" opt in bcfList1 track by $index" > <input type="radio" name="buildid" id="buildid" ng-model = $parent.selected ng-value="bcfList1" required> {{ opt }} </li> </ul>

//这是我的controller.js程序

$scope.getDetails =function(data){
        var id=data.id;
        $('#addNode3').modal('show');
        UpgradeService.getDataById(id).then(function(data){
            if(data!=null){
               $scope.List1=data.BUILDNUMBER;
            }
        });
    }

我需要获取将在单选按钮前列出的字符串值。所以一旦我点击单选按钮,它应该将该值发送到controller.js通过使用ng-model我需要一个解决方案。帮帮我!!

javascript html angularjs angularjs-ng-model jsonresponse
2个回答
0
投票

您需要通过调用该函数将ng-change添加到输入字段。这是一个快速演示:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.b = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  $scope.getDetails = function(index) {
    console.log("sending data", index,$scope.selected);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <div ng-repeat="a in b track by $index">
    <input type="radio" ng-model="$parent.selected" ng-value="a" ng-change="getDetails($index)" /> {{a}}
  </div>
</div>

0
投票

如果我理解正确,您需要收集在控制器中单击的输入类型无线电并将此信息发送到后端。

ng-model指令在这里是非常好的方法,您可以像这样使用它:

HTML

<label>
  One
  <input type="radio" value="one" ng-model="radio">
</label>
<label>
  Two
  <input type="radio" value="two" ng-model="radio">
</label>

<br><br>{{ radio }}<br>

JS

app.controller('MainCtrl', function($scope) {
  $scope.radio = '';
  $scope.consoleLogRadio = function() {
    console.log($scope.radio);
  }

});

看看plunker example

© www.soinside.com 2019 - 2024. All rights reserved.