简体   繁体   中英

Get value of checked radio button in radio button in Angular JS which as ng-repeat

Here is the code on selection of any one of the radio button i need to get the value

<label ng-repeat="SurveyType in SurveyTypes">
    <input type="radio" name="SurveyTypeName" ng-model="surveyData.SurveyTypeName" ng-value="{{surveyData.SurveyTypeName}}" />
    {{SurveyType.Name}} &nbsp; &nbsp; &nbsp;
</label>

You should assign value from your repeat-loop not from model value and no need to use {{}} for ng-value

so use ng-value="SurveyType.Name" instead of ng-value="{{surveyData.SurveyTypeName}}" so selected radio button value set to surveyData.SurveyTypeName .

If you want to select anyone by default you can assign value to surveyData.SurveyTypeName like $scope.surveyData={SurveyTypeName: 'second'} then that radio button shown as selected that has value second .

HTML:

<label ng-repeat="SurveyType in SurveyTypes">
    <input type="radio" name="SurveyTypeName" ng-model="surveyData.SurveyTypeName" ng-value="SurveyType.Name" />
    {{SurveyType.Name}} &nbsp; &nbsp; &nbsp;
</label>

PLUNKER DEMO

Your HTML should be like this.

 <input type="radio" name="SurveyTypeName" ng-model="surveyData.SurveyTypeName" ng-value="{{surveyData.SurveyTypeName}}"  ng-change="getval($index)"/> 

Js

$scope.getval = function (index){


 var servetypename =SurveyTypes[index];

var data =servetypename.SurveyTypeName

}

Don't know from surveyData.SurveyTypeName is coming from.

<li ng-repeat="SurveyType in SurveyTypes">
        <input type="radio" name="SurveyTypeName" ng-model="$parent.rdoSelected" ng-value="SurveyType.SurveyTypeName" />
        {{SurveyType.Name}}
</li>

PLUNKER

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM