I reference the document here: ng-checked
Here, at the example provided at the bottom of the page, you can see that clicking on the first checkbox also checks the second box.
The behaviour that I find with this is that even when the first checkbox is checked, the second checkbox can be unchecked - that is, after checking the first checkbox (the Master checkbox), the second checkbox gets checked automatically. If you now try to uncheck the second checkbox, it works which I find strange. The ng-checked directive is bound to the master model which is still true.
Why then is it possible to uncheck the second box? Shouldn't the ng-checked ensure that the checked state is always in sync with the expression - that is, shouldn't the ng-checked NOT allow the second checkbox to be unchecked as long as the first checkbox is still checked?
How does one manage to keep the second box checked as long as the master checkbox is checked?
Reference Code:
Check me to check both: <input type="checkbox" ng-model="master">
<br/>
<!-- Expecting the following input to be checked as long as the master checkbox
is checked / User should not be able to uncheck this as long as master
is checked -->
<input id="checkSlave" type="checkbox" ng-checked="master">
The ng-checked directive likely (I didn't examine the Angular source code) sets up a $watch on the master property. Only when that property changes will the $watch fire. So when you change the slave checkbox, the $watch doesn't fire, so the change happens.
If you want to keep the second checkbox in sync with the first, add an ng-model to the slave,
<input type="checkbox" ng-model="slave" ng-checked="master">
then force a sync anytime either model changes:
$scope.$watch(
function() { return $scope.master + $scope.slave; }
,function() { $scope.slave = $scope.master; }
);
Fiddle
Note that this will also work:
$scope.$watch('master + slave', function() {
$scope.slave = $scope.master;
});
Related
<select ng-model="model.typeId" ng-show="AllowToChangeType(model)"
ng-options="item.value as item.text for item in model.types">
</select>
The issue is that when the user clicks Save button, the form is regenerated to non-editable mode, AllowToChangeType() returns false, and as this <select> becomes hidden, model.typeId gets set to null. Then, if the user clicks on Edit again, this <select> is regenerated, but nothing is selected in it, although the data has been saved correctly. Ideally, the <select> should show the saved value. How to solve this issue?
I think you are making some incorrect assumptions. ng-show does not remove the model value when the element is hidden, as this plunker proves: https://plnkr.co/edit/knnLRZcHdQukGawU8iyJ?p=preview
ng-show/ng-hide do not regenerate DOM elements, they simply change visibility property
I have a list of radio buttons using ng-repeat.
When I initially load the page and select on the last radio button, the button on it's side gets selected.
This only happens for the first time, i.e. if I click on the last button a second time, it is selected correctly.
<input type="radio" name="rb" value="{{rbCollection.name}}" data-ng-click="open(name)" data-ng-model="Ctrl.selection">
Check this so.
The problem is that ng-click fires before the ng-model code that updates the scope. If you change it to use ng-change, that will fix it. You also need to add ng-change to your text box to update the scope as the user types.
I was trying to use checkbox and bind the checked attribute using ng-checked attribute but its not working where as its working fine with ng-model attribute with checkbox type inputs.
<!-- not working -->
<input type="checkbox" name="checkedBox"
id="checkedBox11"
ng-checked="isChecked">
<!--working-->
<input type="checkbox" name="checkedBox"
id="checkedBox21"
ng-model="isChecked">
I have created a jsbin to demonstrate the same:
here
Since you are not connecting the checkbox with a model in the first case, it is not getting changed in angular and hence the value is not changing in the view also.
However,in the second case, you have attached the isChecked to the checkbox, the changes are reflecting.
Update: If you change the default value of isChecked to true, it shows true and the checkbox is also checked on load.
Changing the first input to have model, changes it. You could also use ng-click to change the value. (addming ng-model="isChecked")
<input ng-model="isChecked" type="checkbox" name="checkedBox" id="checkedBox11" ng-checked="isChecked">
Or you could add ng-click="isChecked=!isChecked"
to the checkbox
how to get value of checkbox when checked? My problem is I alrdy had a ng-model for other stuff in my <input>
http://plnkr.co/edit/wyTRa8FVkGaEPX6BgYXb?p=preview
I want to collect the data so that when user clicked submit, I can get the value of checked checkbox.
You need to use ng-model and ng-true-value to track what you have selected.
Declare an array to track values
$scope.selectedValues=[];
Then use the following bindings
<input ng-show="showC || checked" type="checkbox" id="{{$index}}" ng-model='selectedValues[$index]' ng-true-value='{{item.name}}'>
See my updated plunkr http://plnkr.co/edit/KWh5CzZa3OcqKjjhocDz?p=preview
I have this code:
<input type="checkbox" ng-checked="item.selected == 'yes'" ng-click="change()">{{item.selected}}<br/>
http://jsfiddle.net/NmQXp/2/
As you can see, there are three identical checkboxes, with their checked status binded to the value of some string. The catch is that I want to introduce an intermediate "partially selected" state to the checkboxes. In fact what I want to do is use the checkbox to change the value from "pending" to "ongoing" and then to "done" (I'm making a ToDo list).
If you click one of them several times you can see the correct behavior in the others, but the checked status is wrong in the one you are clicking.
¿Is this a bug in Angular Binding, or am I missing something?
Well i tried to fix your fiddle to take care of the checked issue. The behavior was inconsistent because checkbox gets checked when mouse is clicked over it due to the default HTML behavior. I added these lines to prevent the default behavior for checkbox.
if ($scope.item.selected == 'no') {
$scope.item.selected = 'halfway';
$event.preventDefault();
}
Here is the updated fiddle.
http://jsfiddle.net/cmyworld/J27jN/