简体   繁体   中英

AngularJS How would it be possible to update all radio button values to false when one is selected as true?

I'm pulling in from database separate true/false string values. The example that I'm giving is only to illustrate what I'm getting from database in reality that is a string value of either true or false I can't change this.

What I would like to do is have a single radio button display their value as selected if true and not selected if false.

This bit works fine in my answer below, however when a user selects a different button I would like to make that value 'true' and set all the others as 'false'.

I have tried all sorts of ways including ng-checked but that doesn't work with ng-model. I find radio buttons very confusing in angularJS, please could anybody help me to understand what it is that I'm supposed to be doing? Thankyou

    <!doctype html>
<html ng-app="sample" >
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Test</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" href="app/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="app/css/style.css">
    <script src="node_modules/jquery/dist/jquery.min.js"></script>
    <script src="app/bootstrap/js/bootstrap.min.js"></script>
    <script src="node_modules/angular/angular.js"></script>
    <script>
        var MyApp = angular.module('sample', []);

        MyApp.controller('MainCtrl', function($scope) {

            $scope.red = 'true';
            $scope.yellow = 'false';
            $scope.blue = 'false';
            $scope.green = 'false';
            $scope.orange = 'false';
            $scope.purple = 'false';
            $scope.black = 'false';
            $scope.pink = 'false';
            $scope.white = 'false';

        });     
    </script>  
</head>

<body ng-controller="MainCtrl">
    <div class="container" ><!-- ng-repeat="x in alien.colours" -->

        <h3>What colour alien are you?</h3>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="red" />
        <label>Red</label>&nbsp; Value is {{red}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="yellow" />
        <label>Yellow</label>&nbsp; Value is {{yellow}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="blue" />
        <label>Blue</label>&nbsp; Value is {{blue}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="green" />
        <label>Green</label>&nbsp; Value is {{green}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="orange" />
        <label>Orange</label>&nbsp; Value is {{orange}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="purple" />
        <label>Purple</label>&nbsp; Value is {{purple}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="black" />
        <label>Black</label>&nbsp; Value is {{black}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="pink" />
        <label>Pink</label>&nbsp; Value is {{pink}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="white" />
        <label>White</label>&nbsp; Value is {{white}}<br>
    </div>
</body>

</html>

Below is whats happening when you select a different button the values are updated to 'true' button but cant be set to 'false'

例

One way of doing this (a very basic one, that could be improved a lot!) is using an array of objects to do the job for you.

Fiddle: https://jsfiddle.net/t3t6kueL/

In your Javascript, you could do this:

  var MyApp = angular.module('sample', []);

  MyApp.controller('MainCtrl', function($scope) {

    $scope.colors = [{
        name: 'Red', // name => For presentation
        value: 'false' // value => For toggling the group's values
      },
      {
        name: 'Yellow',
        value: 'false'
      },
      {
        name: 'Blue',
        value: 'false'
      },
      {
        name: 'Green',
        value: 'false'
      },
      {
        name: 'Orange',
        value: 'false'
      },
      {
        name: 'Purple',
        value: 'false'
      },
      {
        name: 'Black',
        value: 'false'
      },
      {
        name: 'Pink',
        value: 'false'
      },
      {
        name: 'White',
        value: 'false'
      },
    ]

    // Expose a method to change the group's values
    $scope.selectColor = function(selectedColorName) {
      // Loop through all of the colors
      $scope.colors.forEach(function(color) {
        // If this is what's being set, then set this to true
        // Note: If there are two different objects, with the same name, you will end up with 2 true values here... 
        if (color.name === selectedColorName) {
          color.value = 'true';
        // For all the others, set it to false
        } else {
          color.value = 'false';
        }
      });
    }
  });

This would allow you to move the logic and handling what to present to the application's code, making your HTML a bit leaner, as in:

<div class="container">

  <h3>What colour alien are you?</h3>

  <div ng-repeat="color in colors">
    <input type="radio" name="AliCol" ng-click="selectColor(color.name)" />
    <label>{{color.name}}</label>&nbsp; Value is {{color.value}}<br>
  </div>

</div>

Use a single value to represent the user's selection. See the snippet below for a working example, and see the docs for more info.

 var MyApp = angular.module("sample", []) MyApp.controller("MainCtrl", function($scope) { $scope.alienColor = "blue" // set the initial value here }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.0/angular.min.js"></script> <div ng-app="sample" ng-controller="MainCtrl"> <div class="container"> <!-- ng-repeat="x in alien.colours" --> <h3>What colour alien are you?</h3> <input type="radio" name="AliCol" value="red" ng-model="alienColor" /> <label>Red</label>&nbsp; Value is {{red}} <br> <input type="radio" name="AliCol" value="yellow" ng-model="alienColor" /> <label>Yellow</label>&nbsp; Value is {{yellow}} <br> <input type="radio" name="AliCol" value="blue" ng-model="alienColor" /> <label>Blue</label>&nbsp; Value is {{blue}} <br> <input type="radio" name="AliCol" value="green" ng-model="alienColor" /> <label>Green</label>&nbsp; Value is {{green}} <br> <input type="radio" name="AliCol" value="orange" ng-model="alienColor" /> <label>Orange</label>&nbsp; Value is {{orange}} <br> <input type="radio" name="AliCol" value="purple" ng-model="alienColor" /> <label>Purple</label>&nbsp; Value is {{purple}} <br> <input type="radio" name="AliCol" value="black" ng-model="alienColor" /> <label>Black</label>&nbsp; Value is {{black}} <br> <input type="radio" name="AliCol" value="pink" ng-model="alienColor" /> <label>Pink</label>&nbsp; Value is {{pink}} <br> <input type="radio" name="AliCol" value="white" ng-model="alienColor" /> <label>White</label>&nbsp; Value is {{white}} <br> </div> {{ alienColor }} </div> 

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