【问题标题】:Not getting values of radiobutton using angularJS没有使用angularJS获取单选按钮的值
【发布时间】:2019-04-12 18:36:31
【问题描述】:

我想在变量中设置选中的radiobutton 的值。但我无法得到它。以下是我尝试过的代码

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>

让我知道我的代码出了什么问题UNDEFINED

【问题讨论】:

    标签: javascript html css angularjs


    【解决方案1】:

    这是因为ng-value 正在寻找“全部”$scope 变量,但很可能没有。 如果预期值是字符串,请使用'All',或者直接使用value

    下面的工作小提琴,点击“下载”按钮记录当前值(先选择一个值)。

    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>

    【讨论】:

    • 完美的伴侣,它奏效了。我只是错过了我猜的价值属性。我正在考虑做类似ng-click 并获取当前值
    • 只是 ng-value 和 ng-click 一样,需要一个表达式。在这种情况下,ng-value 会查找 $scope 属性,因此如果您只需要绑定一个字符串,它将无法按预期工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多