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;
}
Related
I have some input checkbox which is created inside ng-repeat
<li ng-repeat="item in tasks track by $index">
<input type="checkbox" ng-init="setChecked(item.status)"
ng-checked="changeStatus(item.id)"
ng-model="item.status" value="{{item.id}}">
<span ng-model="item.title" ng-keyup="completeEdit(item.id)"
contenteditable="false" ng-dblclick="contentEdit()"
class="done-{{item.status}}">{{ item.title }}
</span>
</li>
What I am trying to achieve is, based on a data item property the checkbox should show either checked or unchecked. For that I am using ng-init of the checkbox. Below is the function I am calling in ng-init
$scope.setChecked = function(status) {
event.target.checked = status;
};
But the problem is the above function is not making change to checkbox. The checkbox is always showing unchecked even if the status is true
Can anyone tell me what I am doing wrong in the above code?
Any help is appreciated :)
To make the checkbox checked or unchecked I should have used ng-checked and for update the status I used ng-change. This fixed the problem for me.
<input type="checkbox" ng-change="changeStatus(item.id)"
ng-checked="item.status" ng-model="item.status"
value="{{item.id}}">
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>
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"
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/
I have a list of check boxes...
<div class="checkBoxGroup" ng-repeat="service in getAdditionalServices()">
<input ng-click="additionalServiceClick($index)" type="checkbox" value="{{service.text}}"><span class="fieldTitle">{{service.text}}</span>
</div>
Event handler...
$scope.additionalServiceClick = function ($index) {
};
In the 'additionalServiceClick' function I want to update an array of checked services. But angular unselects any attempt to check a check box if an ng-click handler is assigned.
How to get Angular to bog off in this instance?