Radio button checked by default when using ng-repeat - angularjs

I have been wanting to have a radio button checked out of a list of radio buttons that I present in the screen using ng-repeat, but my code does not work. This is what I am doing:
<div class="clubRole" data-ng-if="club.checked">
<div data-ng-repeat="role in securityGroups.slice(0,1)">
<input type="radio" class="clubRole" data-ng-model="club.role" data-ng-value="role.securityGroupCode" checked="checked"> {{role.description}}
</div>
<div data-ng-repeat="role in securityGroups.slice(1,securityGroups.length+1)">
<input type="radio" class="clubRole" data-ng-model="club.role" data-ng-value="role.securityGroupCode"> {{role.description}}
</div>
</div>
The intention of the code is to get the first radio button checked, and the others unchecked. That code has a problem: it does not work. But at least it gives the idea of what I am trying to do: I want one of the radio buttons checked by default, no matter which one it is.

Radio button will be check if the value of the input attribute is equal to the value of modal applied on the radio button.
<div ng-repeat="val in ['a','b','c']">
<input
type="radio"
name="val"
data-ng-model="role"
data-ng-value="val"
ng-init="$index==0?(role=val):''"
/>
{{val}}
</div>
Checked="checked" will not work in angular context. You can either set the value of radio button explicitly in controller or you can manage it in the view itself as i did in the above example.But the modal should be equate according to the value attribute on the inmput element.
For example if modal is x on three radio button's and each radio button have different value like a,b and c. then x must be equal to any of the value to be checked.
Plunker

You don't need to worry about checked.
HTML:
<div ng-app="app">
<div ng-controller="mainController">
<label ng-repeat="option in options">
<input type="radio" ng-model="$parent.selected" ng-value="option"/>{{option}}
</label>
</div>
</div>
JavaScript:
var appModule = angular.module('app', []);
appModule.controller('mainController', function($scope) {
$scope.selected = 'red';
$scope.options = ['red', 'blue', 'yellow', 'green'];
});
Working fiddle here: https://jsfiddle.net/qnw8ogrk/1/

You don't need checked="checked", I think angular will take care of it itself if you set the model to one of the values. Something like:
club.role = securityGroups.slice(0,1)[0].securityGroupCode;
Also, the scope may trip you up here, the model may have to be $parent.club.role

if you use a primitive type in list, the answer of bm1729 is correct, but if you use objects in the list, then look this example: https://jsfiddle.net/9chd58mk/2/
the first part is bad, because the selected object is look like same with a list item, but it doesn't same by reference. the == operator is false in this case
$scope.selected = {c:'red'};
$scope.options = [{c:'red'}, {c:'blue'}, {c:'yellow'}, {c:'green'}];
but the second and third examples are use the list item reference, and the radio button checked. the == operator is true in this case
$scope.options3 = [{c:'red'}, {c:'blue'}, {c:'yellow'}, {c:'green'}];
$scope.selected3 = $scope.options3[0];

<md-radio-group ng-model="data.group3">
<md-radio-button value="{{o._id}}" class="md-primary" ng-repeat="o in lstDataRecord" ng-click="updTemplateId(o._id)">
<div flex-gt-sm="50" style="height: 150px; width: 250px;">
<img src="{{PageInfo_PATH}}{{o.imgUrl}}" />
{{o.displayName}}
</div>
</md-radio-button>
<md-radio-button value="active" class="md-primary" [checked]='true'> </md-radio-button>
</md-radio-group>

Related

how to check checkbox whether true or false in angularjs using index value

I have checkbox inside the ng-repeat I want check whether checkbox is checked or not using their index value
html
<div ng-repeat="Name in Names">
<input type="checkbox"
ng-change="checkchange(Name .MaterialStream, $index)"
ng-model="Name.MaterialStream" />
</div>
controller
$scope.checkchange=function(index){
$scope.Names[index].active='true';
}
Now I get correct value when I check the checkbox. I have to get active value is true but I have check means active should be change into false in my case it's true here I attached my code help how to do this.
you can bind Name.active directly without using ng-repeat.
<div ng-repeat="Name in Names">
<input type="checkbox" ng-model="Name.active" />
</div>
Since you are binding Name.MaterialStream to checkbox, you can set Name.active based on it in ng-change.
$scope.checkchange=function(index){
$scope.Names[index].active = $scope.Names[index].MaterialStream;
}
ng-model is more than enough to store true false for checkbox, you don't require $index at all, but If you must do it by $index, then also you can utilize the ng-model.
See this Fiddle example out of your code.
In addition: in your above code, definition of checkchange() method doesn't accept the first argument, thats an issue. you shouldn't pass it if you not going to consume it.
To get the value of your checkbox, you simply have to return the model itself.
HTML
<div ng-repeat="Name in Names">
<input type="checkbox" ng-change="checkchange($index)"
ng-model="Name.MaterialStream" />
</div>
JS
$scope.checkchange=function(index){
return $scope.Names[index].MaterialStream;
}
Note that I removed the first argument you passed to the checkchange function as it wasn't used before. In fact, before you checked the index variable but it wouldn't hold the real $index-value.
Here is your Solution . With working example
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.Names = [{value : true}, {value : true},{value : false},{value : false},{value : true}]
$scope.toggleSelection = function(x,index,event) {
// how to check if checkbox is selected or not
//alert(index + ' is ' + event.target.checked);
$scope.Names[index].Active = $scope.Names[index].value;
};
});
<body ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="Name in Names">
{{$index}}
<input type="checkbox" id="" name="check_box"
value="{{x}}"
ng-change="toggleSelection(x.value,$index,$event)"
ng-model="Name.value" > {{Name.Active}}
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

Set a checkbox in a ng-repeat to checked by default

I have the below checkbox that is in a ng-repeat where this should always be checked by default. The issue that I am running into is that I can set it to checked at the controller scope but not at the item (perf) scope. So if I set it at the controller scope, if you unchecked it, all of the items (perfs) unchecked. BTW, from all of the examples that I have come across show the checkbox being initially checked based on a value in the item array which I don't have.
<div ng-repeat="(perf) in $ctrl.filteredPerfs">
<div class="row">
<div class="pull-right cs-fg" ng-class="{'hidden': perf.showContigSeats == 'N'}">
<md-input-container class="cs_ic">
<span>
<md-checkbox md-block class="ada-reserve" ng-model="perf.resTkts"></md-checkbox>
</span>
<span class="cs-ada-label">Only reserve tickets next to each other</span>
</md-input-container>
</div>
</div>
</div>
Solution 1
Initialize perf.resTkts as true:
<md-checkbox md-block ng-model="perf.resTkts" ng-init="perf.resTkts = true"></md-checkbox>
Solution 2
Use ng-checked:
<md-checkbox md-block ng-model="perf.resTkts" ng-checked="true"></md-checkbox>
ng-checked : If this expression evaluates as truthy, the 'md-checked' css class is added to the checkbox and it will appear checked
<md-checkbox md-block class="ada-reserve" ng-checked="perf.resTkts" ng-model="perf.resTkts"></md-checkbox>
set perf.resTkts = true for default
<md-checkbox md-block class="ada-reserve" ng-checked="true" ng-model="perf.resTkts"></md-checkbox>

Angular - ngClass being added to each clicked radio button (instead of ONLY clicked one)

In my angular app I'm using ng-repeat to generate several radio buttons, the ultimate goal is to add a choiceSelected class (ngClass colour styling) to the label of the clicked radio input. I can add the style to the clicked button, BUT that style is added to every other clicked radio button and I end up with 5 coloured labels. Can't understand why.
Here is the HTML code:
<div class="row" ng-repeat="obj in questionObject.choices">
<div class="radio">
<label class="choice" ng-class="{'choiceSelected': isChecked==$index}">
<input type="radio" name="optradio" ng-model="isChecked" ng-value="$index" ng-click="selectAnswer($index,questionObject)">{{obj.body}}</label>
</div>
</div>
<div class="row" ng-repeat="obj in questionObject.choices">
<div class="radio">
<label class="choice" ng-class="{'choiceSelected': isChecked==$index}">
<input type="radio"
name="optradio"
/*********************************************/
ng-model="isChecked"
ng-value="$index"
/*********************************************/
ng-click="selectAnswer($index,questionObject)">{{obj.body}}
</label>
</div>
</div>
Look at the lines inside the comments, clearly tells that your value of the checkbox($index) is binding to the isChecked variable.
By your, condition in ng-class isChecked==$index it is same for all the elements and so the class is applied for all radio buttons.
if you can update the Json of questionObject , can give you alternative option.
Assuming that questionObject contains the following json
[{
"body": "abc"
}, {
"body": "def"
}, {
"body": "aghi"
} ]
You can use the below code to make your result achieve
<div class="row" ng-repeat="obj in questionObject track by $index" >
<div class="radio" >
<label class="choice"
ng-class="{'choiceSelected': isChecked==$index}">
<input type="radio"
name="optradio"
ng-value="obj.body"
ng-click="selectAnswer($index,obj)">
{{obj.body}}
</label>
</div>
The method selectAnswer should make your work simple. Use this
$scope.selectAnswer=function(number,obj)
{
$scope.isChecked=number;
}
LIVE DEMO
NOTE: In your code the selectAnswer method call in HTML passes the
questionObject fully
Update 1
Reason for manually defining isChecked
Angular looks for the the value of isChecked in the scope because you have used ng-class in the

Angular JS & Semantic-UI checkbox needs double click

I try to operate with AngularJS on checkbox made with Semantic UI:
<div class="ui checkbox" ng-click="myCtrl.value=!myCtrl.value">
<input type="checkbox" class="hidden" ng-model="myCtrl.value"/>
<label>Property</label>
</div>
It looks, like any copy of my Semantic-ui checkbox needs to be "activated" by a click. First click is changing the value, but not the appearance. Then my Semantic-ui checkbox is always one step behind - it shows the opposite state to the one saved under "value" variable.
I noticed, that as I use "normal" checkbox, clicking on the label works fine, but clicking on the input change it's state twice (returns to the first state):
<div ng-click="myCtrl.value=!myCtrl.value">
<label><input type="checkbox" ng-model="myCtrl.value"/> Property</label>
</div>
This is probably something really basic, but as I am working on my first Angular project ever, really basic things are still really big problems:) Thank you.
I simply removed the hidden class on the input
<link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.4/semantic.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.4/semantic.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="ui checkbox" ng-click="myCtrl.value=!myCtrl.value">
<input type="checkbox" ng-model="myCtrl.value" />
<label>Property</label>
</div>
Semantic's JS often doesn't play nicely with Angular.
You may need to set the checkbox's appearance explicitly in the controller.
<div class="ui checkbox" ng-click="setValue()">
<input type="checkbox" class="hidden" ng-model="myCtrl.value"/>
<label>Property</label>
</div>
And then in your controller:
myCtrl.setValue = function() {
myCtrl.value = !myCtrl.value; // sets value
$('.ui.checkbox').checkbox(myCtrl.value ? 'check' : 'uncheck'); // updates checkbox appearance to match
}

AngularJS - using ng-model on a checkbox and toggling it on/off through the controller

Please see jsFiddle
In this simple example here I want to set the checkbox value to true or '1' using a controller. I also want to keep a variable called $scope.button within the controller that is tied to the checkbox using ng-model.
However I am unable to set the checkbox value to true or 1 using $scope.button = 1
<div ng-app="App">
<div ng-controller="MyCtrl">
<input ng-model="$scope.button" ng-true-value="1" ng-false-value="0" type="checkbox"/>
</div>
</div>
Angular code
var app = angular.module('App', [])
app.controller('MyCtrl', function($scope) {
$scope.button = 1;
});
Use this:
<div ng-app="App">
<div ng-controller="MyCtrl">
<input ng-model="button" type="checkbox"/>
<span ng-bind="button"></span>
</div>
</div>
I've removed $scope from the ng-model attribute as this doesn't belong there. It's already being bound to the scope.
Working fiddle: http://jsfiddle.net/4bdkkenp/3/

Resources