Why in Angularjs on selecting one checkbox, all checbox getting selected? - angularjs

Hey guyz i am having issue related to checkboxes in html, i am using angularjs.
Whenever i checked one checkbox other checkbox getting selected and vice-versa.
Here's my Html code.
<form ng-show='MyForm'>
<div ng-controller="MyController">
<div name="sampleName" ng-repeat="sample in list" >
<input ng-model="q.sample" type="checkbox" >{{sample.name}}</label>
</div>
</div>
<button ng-click="submitForm()" >Submit</button>
<button ng-click="cancelForm()">Cancel</button>
</form>
But instead of using scope variable name 'q.sample', if i use only $scope.sample then it is working fine.
Still there is another issue with this too, On submitting data my form gets closed, but when i open it again, it shows me fresh form, there is no checked field, but if i cancel the form instead of submitting it with checked values and again opened it , i dont want the checked values to be present but i am getting the checked values instead of fresh form.
I tried to make values false in checkbox field on cancel button . See my Code of cancelForm()
$scope.cancelForm = function() {
$scope.MyForm = false
$scope.q.sample = false
}
So Basically, i have two questions, one is why i am getting all checkboxes selected on selected only one checkbox when i am using
$scope.q.sample
Second when i am using only
$scope.sample
scope value of sample is not reflecting on UI though it is reflecting in JS, i am getting the checked checkboxes.

It is because you are binding ng-model to the same controller property with this:
ng-model="q.sample"
Instead, if you want to select each individually then you need to have a different ng-model for each checkbox.
At the moment, you are changing q.sample and this, then, binds the value of q.sample to all the other checkboxes that define ng-model="q.sample".
Try something like this instead:
<div name="sampleName" ng-repeat="item in list" >
<input ng-model="item.checked" type="checkbox" >{{sample.name}}</label>
</div>
and then in cancelForm():
$scope.cancelForm = function() {
$scope.MyForm = false
angular.forEach($scope.list, function(item) {
item.checked = false;
});
}

Indeed as David Tryon mentioned, you are assigning the same ng-model to all the checkboxes and what you want is a different ng-model for each checkbox.
You can solve this by assigning a javascript expression to ng-model
for example:
ng-model="list[$index].checked"

Related

AngularJS - Check checkboxes dynamically

I am new to AngularJS. I am working on checkboxlist using WebApi and angularJS and need help.
Well, there is a checkboxlist where user can select multiple options. I am able to write successful code for this. The selected options are saved into the database. But, on edit, I want those options selected already. How can I achieve this?
Thanks.
Here is my checkboxlist:
<div ng-repeat="value in getCheckboxlist">
<input ichecklist type="checkbox" id="{{value.Id}}" value="{{value.Id}}">
<span>{{value.Name}}</span>
</div>
Declaration of array:
$scope.selection: [];
and this is how I am getting selected IDs from database on edit:
$scope.selection: selectedValues;
where 'selectedValues' contains json of selected IDs.
Your are looking for the ngChecked directive from AngularJS
Sets the checked attribute on the element, if the expression inside ngChecked is truthy.
Use it like this
<input id="checkSlave" type="checkbox" ng-checked="expression">
You can replace expression by a call to a function that will verify if this checkbox should be checked or not. The function should return true or false
By using ng-checked you can write it as follow
//Angular Controller codes
$scope.Checkboxlist = [{id:1, value: true, Name: "A"}, {id:2, value: false, Name: "B"}];
//View codes
<div ng-repeat="value in Checkboxlist">
<input ichecklist type="checkbox" id="{{value.Id}}" ng-checked="Checkboxlist[$index].value">
<span>{{value.Name}}</span>
</div>

AngularJS - unchanged data passed by ng-model is interpreted as undefined instead of the value

I'm using Angular to generate some inputs and populate them with data using ng-repeat. I also want to bind the data inside the input to a save changes button which takes parameters provided by ng-model directives. save changes button prints the passed arguments using the built-in JS arguments object. For some reason, unless I change the text inside the input box, the output is [undefined, undefined]. Once I change the text inside the input boxes, the correct output is printed. Why is that?
JSfiddle code.
HTML:
<div ng-app="myApp" ng-controller="MainCtrl">
<p ng-repeat = "man in men">
<label>name</label><input type="text" ng-model="mname" ng-value="man.name"><br>
<label>status</label><input type="text" ng-model="mstatus" ng-value="man.status"><br>
<button ng-click="save(mname,mstatus)">
save changes
</button>
</p>
</div>
JS:
var app = angular.module('myApp', []);
app.controller('MainCtrl', function($scope) {
$scope.men = [{
name: "jon snow",
status: "depands"
}, {
name: "rob stark",
status: "dead"
}];
$scope.save = function() {
console.log(arguments);
}
});
This is not recommended but for your specific requirement you can use ng-init to bind ng-value to your model
<p ng-repeat = "man in men">
<label>name</label><input type="text" ng-model="mname" ng-value="man.name" ng-init="mname = man.name"><br>
<label>status</label><input type="text" ng-model="mstatus" ng-value="man.status" ng-init="mstatus = man.status"><br>
<button ng-click="save(mname,mstatus)">
save changes
</button>
</p>
This wouldn't bind your changes to the original model.
Fiddle
ngModel doesn't update untill you use a key to change it, or set it from your controller. Because you are setting the field of the input using ngValue, it doesn't register to your ngModel untill you change it.
This problem is similar to how most datepickers don't work with ngModel, as they set the field with DOM-manipulation and NOT by inserting the value by "key".
You can easily fix this by using the following HTML instead:
<label>name</label><input type="text" ng-model="man.name"><br>
<label>status</label><input type="text" ng-model="man.status"><br>
I simply removed the ngValue and linked the ngModel to your "man".

How to send all value of checkboxes on server?

I have HTML code is generated by Angular JS via ng-repeat:
<li ng-repeat="(key, item) in data.conditions_list" ng-class="{active: item.active}">
<span class="sbrand-checkbox">
<input ng-checked="item.active" type="checkbox" ng-model="formData.conditions[item.id]">
</span>
<label>{{item.name}}</label>
</li>
This code sets checkboxes on active state if ng-checked="item.active". It works.
When I checked any unchecked element and submit with checked by default form I see:
["conditions"]=>
array(1) {
[5]=>
string(4) "true"
}
So, it means is sent only thta checkbox which I have cheked. Others was not sent on server.
I can do something like as:
HTML:
<input ng-checked="isChecked(item.active, item.id)" type="checkbox" ng-model="formData.conditions[item.id]">
Angular JS:
$scope.isChecked = function (item, id){
if(item.active){
$scope.formData.conditions[id];
}
};
And after send form with $scope.formData.conditions
This is how the browser behaves. It do not submits the unchecked checkbox value. It has nothing to do with angularJs or Javascript.
You can treat the absence of checkbox value in the request as unchecked on the server.
An alternative way is to maintain hidden fields for each checkbox and set its value on checkbox change and submit these hidden fields to the server instead of checkboxes.
I love angular but when it comes to checkboxes and radio boxes they stink. I would resort to jquery. Insert the dom element that contains the inputs, and you will get back an array of values:
function getChecked(dom) {
var boxes = $(dom).find('input[type="checkbox"]').map(function()
{
return $(this).prop("checked")
}
);
return boxes;
}

How to programmatically uncheck checkbox?

I want to programmatically uncheck a checkbox. I know how to it in javascript but since I'm using angular, i think it's different.
Here's the link of jsfiddle : https://jsfiddle.net/TKVH6/499/
This is the first time I used jsfiddle so please let me know if you cant see the script and html.
This is the html
<input type="checkbox" ng-model="v" ng-click="checkAll()" />
<button ng-click="x()">eto</button>
This is the angular
$scope.x = function () {
$scope.v.checked=false;
};
I know there are lots of question like this, I've already tried those but I can't make it work.
Thanks!
<input type="checkbox" ng-checked="v" ng-click="checkAll()" />
In your controller
$scope.v = true; // or false
First thing : You have specified controller on ul and bind click event of button outside the ul so moved ng-controller on div.
Second thing: In order to check it pragmatically you need to set $scope.Items[i].Selected = true;
$scope.x = function () {
alert("x");
$scope.Items[0].Selected=true;
};
Why we need to set Selected property of Items[i] while I have not declared?
The reason behind that is your html binding is something like this:
<li ng-repeat="item in Items">
<label>{{item.Name}}
<input type="checkbox" ng-model="item.Selected" />
</label>
</li>
Here every item is element from Items array that means your checkbox checked value is now bind to selected property of that object. Even though you have not defined that property in Items collection angular will create it and will bind it to that property. So you are required to set that property. I hope it would help you.
Here is working fiddle => link

Angularjs checkbox not binding to initial value

Why does angular and the checkbox model not bind to the checked="checked". The checkbox is unset once angular loads.
eg: http://jsfiddle.net/5CZ74/2/
<div ng-app>
<form>
<input type="checkbox" ng-model="redirect" checked="checked">
<input type="text" ng-disabled="redirect==false">
</form>
</div>
When my form loads I am setting it enabled or disabled on the server as per server entity. How do I get the angular model to correctly bind to this value to enable or disable the input text field.
Thanks.
You have not defined redirect in your model anywhere, so it is undefined, and thus falsy. This is why the the checkbox is not checked. If you want it checked you need to add redirect to your model and set it to true. You need to have a controller and do this:
$scope.redirect = true;
See http://jsfiddle.net/5CZ74/3/

Resources