2016-06-03 5 views
0

Ich versuche, den Wert des Optionsfelds zu erhalten. Also habe ich ein Klick-Event geschrieben und in dem Klick übergebe ich den Wert. Wenn der Benutzer auf einen der Optionsschaltflächen klickt, wird nur der zuletzt angeklickte Optionsfeldwert festgelegt. Gibt es einen besseren Ansatz dafür? Wie zum Beispiel, welcher Radiobutton aktiviert ist und den entsprechenden Wert anzeigen.besserer Ansatz, um den Wert der Radio-Schaltfläche zu erhalten, außer Click-Ereignis

JS

;(function() { 
    'use strict'; 

    angular 
     .module('tramsConsole',[]) 
     .controller('TremorController', TremorController); 

    TremorController.$inject = ['$scope', '$log']; 

    function TremorController($scope, $log) { 
     var vm = this; 
     vm.status=''; 
     vm.datametric = ""; 
     console.log("controller loaded"); 
     vm.getstatus = getstatus; 

     function getstatus(obj){ 
      vm.status = obj; 
     } 

    }   
})(); 

HTML

<body ng-app="tramsConsole"> 
    <div ng-controller="TremorController as tremorController"> 
      <input type="radio" name="data" value="NUM_ERRORS" ng-model="processState.widgetInstance.configuration.data.NUM_ERRORS" ng-click="tremorController.getstatus('NUM_ERRORS')">NUM_ERRORS<br> 
      <input type="radio" name="data" value="NUM_UB_OCCURRENCES" ng-model="processState.widgetInstance.configuration.data.NUM_UB_OCCURRENCES" ng-click="tremorController.getstatus('NUM_UB_OCCURRENCES')">NUM_UB_OCCURRENCES<br> 
      <input type="radio" name="data" value="NUM_T_OCCURRENCES" ng-model="processState.widgetInstance.configuration.data.NUM_T_OCCURRENCES" ng-click="tremorController.getstatus('NUM_T_OCCURRENCES')">NUM_T_OCCURRENCES<br> 
      <input type="radio" name="data" value="NUM_OCCURRENCES" ng-model="processState.widgetInstance.configuration.data.NUM_OCCURRENCES" ng-click="tremorController.getstatus('NUM_OCCURRENCES')">NUM_OCCURRENCES<br> 
      <input type="radio" name="data" value="AVG_RSP_TIME" ng-model="processState.widgetInstance.configuration.data.AVG_RSP_TIME" ng-click="tremorController.getstatus('AVG_RSP_TIME')">AVG_RSP_TIME<br> 
      <input type="radio" name="data" value="UB_AVG_RSP_TIME" ng-model="processState.widgetInstance.configuration.data.UB_AVG_RSP_TIME" ng-click="tremorController.getstatus('UB_AVG_RSP_TIME')">UB_AVG_RSP_TIME<br> 
      <input type="radio" name="data" value="T_AVG_RSP_TIME" ng-model="processState.widgetInstance.configuration.data.T_AVG_RSP_TIME" ng-click="tremorController.getstatus('T_AVG_RSP_TIME')">T_AVG_RSP_TIME<br> 
      <input type="radio" name="data" value="UB_SQR_AVG_RSP_TIME" ng-model="processState.widgetInstance.configuration.data.UB_SQR_AVG_RSP_TIME" ng-click="tremorController.getstatus('UB_SQR_AVG_RSP_TIME')">UB_SQR_AVG_RSP_TIME<br> 
      <input type="radio" name="data" value="T_SQR_AVG_RSP_TIME" ng-model="processState.widgetInstance.configuration.data.T_SQR_AVG_RSP_TIME" ng-click="tremorController.getstatus('T_SQR_AVG_RSP_TIME')">T_SQR_AVG_RSP_TIME<br> 
      <input type="radio" name="data" value="SQR_AVG_RSP_TIME" ng-model="processState.widgetInstance.configuration.data.SQR_AVG_RSP_TIME" ng-click="tremorController.getstatus('SQR_AVG_RSP_TIME')">SQR_AVG_RSP_TIME<br> 


    <br> 
    <div> 

    The selected Value is : 

    {{tremorController.status}} 


    </div> 
    </div> 
    </body> 

Working Copy

+0

angularjs hat eine Anweisung namens ** radio **, Sie benötigen nur ein ng-Modell: [hier ist das Dokument] (https://docs.angularjs.org/api/ng/input/input%5Bradio% 5D) – watashiSHUN

Antwort

2

Ich habe Ihre Plunker hier bearbeitet: http://plnkr.co/edit/VAAqiW3jMmAwAROJTg0G?p=preview

Wenn Sie die ng-Modell eingestellt, das gleiche zu sein, den Wert die ausgewählte Taste wird in diesem Modell berücksichtigt werden:

HTML

<div ng-controller="TremorController as tremorController"> 
      <input type="radio" name="data" value="NUM_ERRORS" ng-model="radioButtons">NUM_ERRORS<br> 
      <input type="radio" name="data" value="NUM_UB_OCCURRENCES" ng-model="radioButtons">NUM_UB_OCCURRENCES<br> 
      <input type="radio" name="data" value="NUM_T_OCCURRENCES" ng-model="radioButtons">NUM_T_OCCURRENCES<br> 
      <input type="radio" name="data" value="NUM_OCCURRENCES" ng-model="radioButtons">NUM_OCCURRENCES<br> 
      <input type="radio" name="data" value="AVG_RSP_TIME" ng-model="radioButtons">AVG_RSP_TIME<br> 
      <input type="radio" name="data" value="UB_AVG_RSP_TIME" ng-model="radioButtons">UB_AVG_RSP_TIME<br> 
      <input type="radio" name="data" value="T_AVG_RSP_TIME" ng-model="radioButtons">T_AVG_RSP_TIME<br> 
      <input type="radio" name="data" value="UB_SQR_AVG_RSP_TIME" ng-model="radioButtons">UB_SQR_AVG_RSP_TIME<br> 
      <input type="radio" name="data" value="T_SQR_AVG_RSP_TIME" ng-model="radioButtons">T_SQR_AVG_RSP_TIME<br> 
      <input type="radio" name="data" value="SQR_AVG_RSP_TIME" ng-model="radioButtons">SQR_AVG_RSP_TIME<br> 


    <br> 
    <div> 

    The selected Value is : {{radioButtons}} 

    </div> 
1

Sie eine benutzerdefinierte Richtlinie für immer den Wert des Optionsfeld in der Steuerung schreiben kann.

app.directive('buttonId', function() { 
     return { 
      restrict: "A", 
      link: function (scope, element, attributes) { 
       element.bind("click", function() { 
        scope.selectedRadioButton = attributes.buttonId;     
       }); 
      } 
     } 
    }); 

die Richtlinie in HTML verwenden Sie

können
<input type="radio" name="gender" value="female" button-id="3" > Female<br> 
Verwandte Themen