0

I have two radio buttons and I want the first to be automatically selected

<input type="radio" name="playlist" ng-value="myCtrl.cleanPlaylist" ng-model="myCtrl.playlistSelected"> Clean

<input type="radio" name="playlist" ng-value="myCtrl.explicitPlaylist" ng-model="myCtrl.playlistSelected" ng-disabled="myCtrl.

I have the first one to be selected in my controller but for some reason it's not working...

var myModule = angular.module("MyApp", []).controller('MyController', function(){

this.explicitDisabled = false;

this.playlistSelected = this.cleanPlaylist;

})
1
  • Maybe I'm missing someone but why not just add the checked property to the first? Couldn't believe the other answers didn't say that Commented Jul 8, 2016 at 16:13

2 Answers 2

2

You must set a value for radio button, ng-value is an angular expression to which ng-model will be be set when the radio is selected, according to docs.

Then, to make it works you can do it as following:

(function() {
  "use strict";
  angular.module('app', [])
    .controller('mainCtrl', function() {
      var vm = this;
      
      vm.playlistSelected = 'clean';
    });
})();
<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
</head>

<body ng-controller="mainCtrl as main">
  <input type="radio" name="playlist" value="clean" ng-model="main.playlistSelected"> Clean
  <input type="radio" name="playlist" value="complete" ng-model="main.playlistSelected"> Complete
</body>

</html>

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

Comments

0

playlistSelected is null, it will need a value that matches the value of the radio button. What is cleanPlaylist? Why not just hardcode the value of the radio and then set the playlistSelected to that value in the controller.

Comments

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.