1

I want to set values of checked radiobutton in a variable. But I am unable to get it. Below is the code what I tried

master.controller('FiberLead_Filter', function($scope, $http, NEIQC_Service, Scopes, $rootScope) {
      var rdSelectMP = $scope.MPSelect; // here I am getting undefined
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.25/angular.min.js"></script>
<div class="rightFilter" ng-controller="FiberLead_Filter as Filter" id="FiberLead_Filter">
  <div class="pdfDownload customeRadioWrap">
    <div class="customeRadio">
      <input type="radio" id="btnCurrentMP" name="radio-group" ng-model="MPSelect" ng-value="Current">
      <label for="btnCurrentMP">Current MP</label>
    </div>
    <div class="customeRadio">
      <input type="radio" id="btnAllMP" name="radio-group" ng-model="MPSelect" ng-value="All">
      <label for="btnAllMP">All MP</label>
    </div>
    <button class="btn btn-default customBtn" ng-click="DownloadExcelReport()"><i class="fa fa-file-pdf-o" aria-hidden="true"></i> Download</button>
  </div>
</div>

Let me know what is wrong with my code as I am getting it UNDEFINED

1 Answer 1

2

This is because ng-value is looking for the "All" $scope variable, but there likely is none. If the expected value is a string, either use 'All' or, instead, just use value.

Working fiddle below, click the "download" button to log the current value (select a value first).

angular.module('app', []);

angular.module('app').controller('FiberLead_Filter', function($scope, $http, $rootScope) {
      $scope.DownloadExcelReport = function(){
        console.log($scope.MPSelect);
      }
    }
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.25/angular.min.js"></script>
<div ng-app="app">
  <div class="rightFilter" ng-controller="FiberLead_Filter as Filter" id="FiberLead_Filter">
    <div class="pdfDownload customeRadioWrap">
      <div class="customeRadio">
        <input type="radio" id="btnCurrentMP" name="radio-group" ng-model="MPSelect" value="Current">
        <label for="btnCurrentMP">Current MP</label>
      </div>
      <div class="customeRadio">
        <input type="radio" id="btnAllMP" name="radio-group" ng-model="MPSelect" value="All">
        <label for="btnAllMP">All MP</label>
      </div>
      <button class="btn btn-default customBtn" ng-click="DownloadExcelReport()"><i class="fa fa-file-pdf-o" aria-hidden="true"></i> Download</button>
    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

perfect mate, it worked.I was just missing the value attirbute I guess. I was thinking of doing something like ng-click and get the current value
it's just that ng-value, like ng-click, takes an expression. In that case, ng-value looks for $scope property, so it won't work as expected if you simply need to bind a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.