Multiple dynamic input controls with ng-repeat in Angular JS - angularjs

I have to develop a configuration screen where I have to fetch a set of key pair values from db and show in the UI to update the configuration. Here, when the value is 'TRUE' or 'FALSE', I have to show the input control as checkbox and for the rest of the values, I have to show the input control as textbox. I have used ng-repeat for single input control. But here I need to show two input controls (checkbox / textbox) based on the value. Can you please give me an idea on how to use ng-repeat to implement with multiple input controls ?

How about something like this:
<div ng-repeat="item in items"
ng-init="item.showCb = item.value == 'TRUE' || item.value == 'FALSE'">
<input type="checkbox"
ng-if="item.showCb" />
<input type="text"
ng-if="!item.showCb" />
</div>
JSFIDDLE

Related

Angular Radio Buttons Form Validation

I'm generating a group of radio buttons options from a JSON object which is requested when another radio button is selected. This works as expected but the form validation is not working correctly since the form only becomes valid if all the options in the radio button group are first clicked.
My mark-up for the radio button:
<div data-ng-repeat="option in options" class="radio">
<label>
<input type="radio" name="decline_type_id" ng-model="decline_type_id" value="{{option.id}}" ng-required="!decline_type_id" />
<strong>{{option.name}}</strong>
</label>
</div>
Here is my plunker:
https://plnkr.co/edit/B7KgUt4GMrnSATYIQuN5?p=preview
The same mark-up without the loop works as expected, so I don't understand what it is about the loop used to generate the list that is breaking the validation of the form until all options are selected?
This is a Angular scoping issue. The ng-model inside the ng-repeat is using a child scope.
The ng-model="decline_type_id" was unique for each iteration of the loop. So ng-required check was for each unique model.
You can make use of $parent scope to do the ng-required checking on a shared variable instead.
<input type="radio" ng-model="$parent.decline_type_id" value="{{option.id}}" ng-required="!$parent.decline_type_id" />

AngularJS ng-repeat filter not updating on keypress only on enter

I have a ng-repeater and a textbox which filters this repeater. I want the filter to happen as you type at the moment it works when you press enter or tab off the search textbox. I thought this was the default behavior but it does not seem to work, not sure if there is a browser setting or something?
The code is as follows
<input type="text" ng-model="vm.filterText" />
<div ng-repeat="product in vm.products | filter: vm.filterText">
Where vm is my bound view model

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

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"

Dynamically added radio inputs does not trigger ng-show

Okay guys, here is my problem:
I have to make list of categories which show only elements of that category when selected ( and hide rest ). I have decided to go with radio buttons / every category has its own radio button.
Code for adding categories is next:
APP.js
controller('FamiliesCtrl', function($scope) {
$scope.families = ['All', 'Stark', 'Lannister'....];
$scope.selfamily = {selfam: 'All'};
}).
HTML
<form ng-controller="FamiliesCtrl">
<input type="radio" ng-model="selfamily.selfam" ng-repeat="family in families" ng-value="family" name="selfam">
</form>
And I am trying to trigger ng-show in html which has next code:
<div id="characters" class="row col-xs-9" ng-controller="CharacteresCtrl">
<div class="character col-xs-3" ng-repeat="character in characters | orderBy:sortParameter | limitTo: 120" char-image="{{character.picture}}" ng-show="selfamily.selfam === '{{character.house}}' || selfamily.selfam === 'All'">
<h1>{{character.name}}</h1>
</div>
</div>
Okay, now. This doesn't work BUT if I insert categories manually in this format:
<input class="checkboxHidden" id="first" type="radio" name="selfam" ng-model="selfamily.selfam" value="All">
... and so on...
IT DOES WORK. Your help would be much appreciated. If I need to provide any more code, please let me know.
Just to sum up, dinamically/programatically added category does not have any power on ng-show while manually added works just fine...
You don't need interpolation to access the value of character.house. This should work:
ng-show="selfamily.selfam === character.house || selfamily.selfam === 'All'"
...
Looking closer at your code, the values set by your radio buttons exist in a different controller than the list of characters. You'll have to use a single controller for all values, or use a service to share values between controllers.

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