how to create dynamic inputs using ng-repeat and $index - angularjs

I am trying to iterate over array of questions. But I need to attach radio button answers to each question.
In this case I need to add different ng-models for each answer.
I tried the code below but it didn't work.
Is this the correct way of using $index of the ng-repeat?
<li class="fields" ng-repeat="question in questions">
<p>question:{{question.title}}</p>
<div class="radio-buttons">
<input type="radio" id="radio01" value="0" ng-model="answer[$index]" checked="checked"/>
<label for="radio01"><span></span>Yes</label>
<input type="radio" id="radio02" value="1" ng-model="answer[$index]"/>
<label for="radio02"><span></span>No</label>
</div>
</li>

you need to do it like this modelname{{$index}}
<li class="fields" ng-repeat="question in questions">
<p>question:{{question.title}}</p>
<div class="radio-buttons">
<input type="radio" id="radio01" value="0" ng-model="answer{{$index}}" checked="checked"/>
<label for="radio01"><span></span>Yes</label>
<input type="radio" id="radio02" value="1" ng-model="answer{{$index}}"/>
<label for="radio02"><span></span>No</label>
</div>
</li>

You can also do it via square bracket notation too if that pleases you aesthetically more:
<li class="fields" ng-repeat="(i, question) in questions">
<p>question:{{question.title}}</p>
<div class="radio-buttons">
<input type="radio" id="radio01" value="0" ng-model="answer[i]" checked="checked"/>
<label for="radio01"><span></span>Yes</label>
<input type="radio" id="radio02" value="1" ng-model="answer[i]"/>
<label for="radio02"><span></span>No</label>
</div>
</li>
Also to note, models are better when expressed as dot models. So for example:
<input type="radio" ng-model="answer[i]" />
Would be better as:
<input type="radio" ng-model="models.answer[i]" />
Then in your controller this makes it simple to list all the models via the object 'models', rather than finding out how many answer[i] models there are.

Related

How to show auto selected to radio buttons when it comes from Loop?

I have a small application of attempt online exam. My questions set are comes from DB using loop. I want to edit my already attempted exam. I have a answer set of questions. But I am not able to show auto select to attempt radio buttons.
Here is my answer set
{"1":"A","2":"A","6":"A","10":"A","14":"A","21":"B","26":"C","31":"B","33":"C","34":"B","54":"C"}
Here my questions which I am showing on view page using loop
<div class="row" style="margin-top: 20px;">
<section ng-repeat="count in subject.test_count" ng-cloak class="col col-3">
<div class="inline-group" ng-if="count%2!='0'">
<b style="width: 25px;display: inline-grid;">{{count}}</b>
<input type="radio" name="{{subject.name}}{{count}}"
id="a{{subject.name}}{{count}}" class="input-hidden"
ng-model="questiondata[subject.name][count]"
value="A" ng-checked="true">
<label for="a{{subject.name}}{{count}}" >
<p>A</p>
</label>
<input type="radio" name="{{subject.name}}{{count}}"
id="b{{subject.name}}{{count}}" class="input-hidden"
ng-model="questiondata[subject.name][count]" value="B">
<label for="b{{subject.name}}{{count}}" >
<p>B</p>
</label>
<input type="radio" name="{{subject.name}}{{count}}"
id="c{{subject.name}}{{count}}" class="input-hidden"
ng-model="questiondata[subject.name][count]" value="C">
<label for="c{{subject.name}}{{count}}" >
<p>C</p>
</label>
<input type="radio" name="{{subject.name}}{{count}}"
id="d{{subject.name}}{{count}}" class="input-hidden"
ng-model="questiondata[subject.name][count]" value="D">
<label for="d{{subject.name}}{{count}}" >
<p>D</p>
</label>
<input type="radio" name="{{subject.name}}{{count}}"
id="e{{subject.name}}{{count}}" class="input-hidden"
ng-model="questiondata[subject.name][count]" value="E">
<label for="e{{subject.name}}{{count}}" >
<p>E</p>
</label>
</div>
</section>
There are a couple things that are off with this. First is that you are ngRepeating through the values of the test_count and not the actual objects, to resolve that you're going to want to do something like this:
<section ng-repeat="(key,value) in subject.test_count" ng-cloak class="col col-3">
{{key}} {{value}}
<span class="inline-group" ng-if="count%2!='0'">
<input type="radio" name="{{subject.name}}{{key}}"
id="a{{subject.name}}{{key}}" class="input-hidden"
ng-model="questiondata[subject.name][key]"
value="A">
<label for="a{{subject.name}}{{key}}" >A</label>
<input type="radio" name="{{subject.name}}{{key}}"
id="b{{subject.name}}{{key}}" class="input-hidden"
ng-model="questiondata[subject.name][key]" value="B">
<label for="b{{subject.name}}{{key}}" >B</label>
<input type="radio" name="{{subject.name}}{{key}}"
id="c{{subject.name}}{{key}}" class="input-hidden"
ng-model="questiondata[subject.name][key]" value="C">
<label for="c{{subject.name}}{{key}}" >C</label>
<input type="radio" name="{{subject.name}}{{key}}"
id="d{{subject.name}}{{key}}" class="input-hidden"
ng-model="questiondata[subject.name][key]" value="D">
<label for="d{{subject.name}}{{key}}" > D</label>
<input type="radio" name="{{subject.name}}{{key}}"
id="e{{subject.name}}{{key}}" class="input-hidden"
ng-model="questiondata[subject.name][key]" value="E">
<label for="e{{subject.name}}{{key}}" >E</label>
</span>
</section>
Once you have your radio buttons and subject matching (I took some liberty with how I think you'd have the data setup) you need to seed the questiondata using the values you've provided above:
$scope.questiondata[0] = angular.copy($scope.subject.test_count);
Now we can see that $scope.questiondata[0][1] = $scope.subject.test_count[1] = 'A' which means that A will be selected.
Here is a sample of the full controller: https://plnkr.co/edit/fyJwfLc6Qb7b7p4ZLgqI

Dynamically generating checkboxes from ng-repeat

I am trying to generate checkboxes with ng-repeat.After selecting a value from dropdown I am getting values based on value selected.But after I select any checkbox only first checkbox is getting checked.I am not able to select other checkboxes.Can anyone tell how to select any one checkbox or more than one so that I can bind that values based on ng-model.
Here is the html code
<div class="form-group" data-ng-repeat="collegesByType in
collegesByTypes">
<div class="be-checkbox">
<input data-ng-model="committee.collegeId" name="collegeId"
type="hidden"
value="0">
<input data-to-date="currently_todate386"
id="currently_checkbox386" class="currently" type="checkbox"
value="1">
<label for="currently_checkbox386">{{collegesByType.name}}</label>
</div>
</div>
Try Like this:
<div class="form-group" data-ng-repeat="collegesByType in
collegesByTypes track by $index">
<div class="be-checkbox">
<input data-ng-model="committee.collegeId[collegesByType.collegeId]" name="collegeId"
type="hidden"
ng-value="collegesByType.collegeId">
<input data-to-date="currently_todate386"
id="currently_checkbox386{{$index}}" class="currently" type="checkbox"
value="1">
<label for="currently_checkbox386{{$index}}">{{collegesByType.name}}</label>
</div>
</div>

vice versa ON/OFF switches in angularjs by

I am using using two ON/OFF switches in my program.When one switch is ON another switch will automatically OFF(vice versa).using angular, give me your suggestion.
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
<label class="onoffswitch-label" for="myonoffswitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
yes it's possible with angular.
- save checkbox state to ng-model
- set selected state according to the model
- you can use a combination of ngTrueValue and ng-model like this
- utilise ng-init to set the initial value of the element
<input type="checkbox" name="someName" value="0" ng-model="someValue" ng-true-value="0" ng-init="someValue=0">
<input type="checkbox" name="someName" value="1" ng-model="someValue" ng-true-value="1">
this will swap the checked state between the two checkboxes.
do message me if I've misunderstood your intention.

Use ng-model with condition

I am using angularjs in my project.
I have below code in my HTML:
<div class="MT10" ng-init="getPaymentDetailsById()">
<div class="panel">
<div class="form-group">
<label> Name:</label>
<span class="rightside">
<input type="text" value="" ng-model="singleGatewayDetails.name" >
</span>
</div>
<div class="form-group">
<label> APP :</label>
<span class="rightside">
<input type="text" value="" ng-model="paymentDetails.id" ng-disabled="appId">
</span>
</div>
<div class="form-group">
<label> APP KEY:</label>
<span class="rightside">
<input type="text" value="" ng-model="singleGatewayDetails.appKey" >
</span>
</div>
<div class="form-group">
<label> APP SECRET:</label>
<span class="rightside">
<input type="text" value="" ng-model="singleGatewayDetails.appSecret">
</span>
</div>
</div>
</div>
now
<span class="rightside">
<input type="text" value="" ng-model="paymentDetails.id" ng-disabled="appId">
</span>
This code displays some data in my textbox. And I want to display that data in textbox in both scenario, that is while editing and while posting new data to server. So it just displays it while editing but not when I want to post new data to server. Basically this textbox will always have some value pre-filled in it. So i am guessing I have to use some condition on that. So please help me how to achieve that.
I think I know what you are getting at. Without seeing your JS file it would be very hard to give you a good example, however using $scope.value in your JS and setting the text to {{value}} in your HTML should provide the desired result.
Now that I understand your question, you could do this:
<input type="text" ng-model="paymentDetails.id" ng-disabled="appId" ng-show="adding"/>
<span ng-hide="adding">{{paymentDetails.id}}</span>
And then in your controller you could control the variable:
$scope.adding = true
and set it to false based on some condition

how to make text gray if switch is off in angular?

Hi here i have on off switch, if the switch is on TEXT should be normal black color, else TEXT should be red color, Some one help me out in this
FIDDLE:
http://jsfiddle.net/0o88p8xy/
**Html code**
<section>
<label>Award of excelence</label>
<div class="onoffswitch">
<input type="radio" id="radio1" name="radios" class="SwitchOn" value="all"
checked>
<label for="radio1"></label>
<input type="radio" id="radio2" name="radios" class="SwitchOff"
value="false">
<label for="radio2"></label>
</div>
</section>
<section>
<label>Innovation Index</label>
<div class="onoffswitch">
<input type="radio" id="radios21" name="radios2" class="SwitchOn" value="all"
checked>
<label for="radios21"></label>
<input type="radio" id="radios22" name="radios2" class="SwitchOff"
value="false">
<label for="radios22"></label>
</div>
</section>
Here are some leads :
Use the ngModel directive to bind your switches to a boolean property of the $scope(so you will need a controller for that).
Use the ngClass directive to reflect the value of that boolean property by having a variable class on the text element.

Resources