Angular Syntax: How to use ng-repeat $index in expression - angularjs

I have following code:
<form name="reviewForm_{{$index}}" ng-controller="reviewController as reviewCtrl" ng-submit="reviewCtrl.addReview(product)" novalidate>
<blockquote class="newReview">
<b>Stars {{reviewCtrl.review.stars}}</b>
<p>{{reviewCtrl.review.comment}}</p>
<cite>{{reviewCtrl.review.author}}</cite>
</blockquote>
<select class="form-control" ng-model="reviewCtrl.review.stars">
<option value="1">1 star</option>
<option value="1">2 stars</option>
<option value="1">3 stars</option>
<option value="1">4 stars</option>
<option value="1">5 stars</option>
</select><br />
<textarea ng-model="reviewCtrl.review.comment" required></textarea>
<label>by:</label>
<input type="text" ng-model="reviewCtrl.review.author" required /><br />
<div>reviewForm is {{reviewForm_$index.$valid}}</div>
<input class="btn btn-primary btn-large" type="submit" value="Submit" />
</form>
The Form above lies within a ng-repeat and has the name reviewForm_$index.
That works fine. So it will give reviewForm_0, reviewForm_1 and so on.
Now I would like to check if the form is valid with {{reviewForm_$index_$valid}}.
For some reason it doesn't show anything.
Do I have the wrong syntax?

Actually you are trying to access a property that does not exists since reviewform_$index will be treated as a form name not a variable. So change your code to something like this:
<div>reviewForm is {{reviewCtrl['reviewForm_' + $index].$valid}}</div>
That will work!!

Ok, I got it to work thanks to #klode.
Here my solution:
<form name="reviewForm" ng-controller="reviewController as reviewCtrl" ng-submit="reviewCtrl.addReview(product)" novalidate>
<blockquote class="newReview">
<b>Stars {{reviewCtrl.review.stars}}</b>
<p>{{reviewCtrl.review.comment}}</p>
<cite>{{reviewCtrl.review.author}}</cite>
</blockquote>
<select class="form-control" ng-model="reviewCtrl.review.stars">
<option value="1">1 star</option>
<option value="2">2 stars</option>
<option value="3">3 stars</option>
<option value="4">4 stars</option>
<option value="5">5 stars</option>
</select><br />
<textarea ng-model="reviewCtrl.review.comment" required></textarea>
<label>by:</label>
<input type="text" ng-model="reviewCtrl.review.author" required /><br />
<div>reviewForm is {{reviewForm.$valid}}</div>
<input class="btn btn-primary btn-large" type="submit" value="Submit" />
</form>
As explained by Klode can I leave the form name without the $index trailer and address the form's $valid property each loop via it's name just so: reviewForm.$valid

Related

Using ng-model and ng-if to show elements based on value - Angular 2

So in AngularJS i was using the following to show specific form elements based on a value that is captured by a model:
<fieldset class="full-width sm-padding">
<label>Do you have any major medical conditions such as
heart conditions, cancer or diabetes?</label>
<div class="inline-flex">
<input type="radio" name="medicalCondition" value="yes"
tabindex="16" ng-model="clientDetails.medicalCondition">Yes<br>
</div>
<div class="inline-flex">
<input type="radio" name="medicalCondition" value="no"
tabindex="17" ng-model="clientDetails.medicalCondition">No<br>
</div>
</fieldset>
<fieldset class="full-width sm-padding" ng-
if="clientDetails.medicalCondition == 'yes'">
<label>Are you currently taking any medication or do you
have any other medical conditions? For example HBP, Cholesterol, Asthma,
Depression? (Both lives if cover for couple)</label>
<div class="inline-flex">
<input type="radio" name="otherMedicalCondition"
value="yes" tabindex="18" ng-
model="clientDetails.otherMedicalCondition">Yes<br>
</div>
<div class="inline-flex">
<input type="radio" name="otherMedicalCondition"
value="no" tabindex="19" ng-
model="clientDetails.otherMedicalCondition">No<br>
</div>
</fieldset>
How can I do the same thing in Angular 2?
Have tried this but not working:
<fieldset class="full-width sm-padding">
<label>Do you smoke?</label>
<div class="inline-flex">
<input type="radio" name="smoker" value="yes"
tabindex="12" [(ngModel)] = "clientDetails.smoker">Yes<br>
</div>
<div class="inline-flex">
<input type="radio" name="smoker" value="no" tabindex="13" [(ngModel)] = "clientDetails.smoker">No<br>
</div>
</fieldset>
<div class="full-width flex" *ngIf="clientDetails.smoker ===
'Yes'">
<fieldset class="one-quarter sm-padding">
<label>What do you smoke?</label>
<input list="whatDoYouSmoke" name="whatDoYouSmoke"
tabindex="14">
<datalist id="whatDoYouSmoke">
<option value="Cigarettes">
<option value="Cigars">
<option value="Pipe">
<option value="E-Cigs">
<option value="Other">
</datalist>
</fieldset>
<fieldset class="one-quarter sm-padding">
<label>How many per day?</label>
<input type="number" min="0" name="howManySmokesPerDay"
tabindex="15">
</fieldset>
</div>
What am I doing wrong?
Have tried so many different options but can't quite get it to work!
thanks
The only thing I can see is that you have typed [(ngModel)] = "", try without spaces like: [(ngModel)]="". Also noticed that in your *ngIf="clientDetails.smoker ===
'Yes'", try with a little 'y' in yes as: *ngIf="clientDetails.smoker ===
'yes'". Since you are setting the value to "yes" and not "Yes" in your radio button.
There is a case problem in the Angular version. The first 'yes' starts with a lower case y and a second 'Yes' with a upper case Y.
For anyone out there looking for a solution to this, I ended up doing the following:
<fieldset class="full-width sm-padding">
<label>Do you smoke?</label>
<div class="inline-flex">
<input type="radio" name="smoker" [(ngModel)]="smoker"
[value]="true" [checked]="smoker" /> Yes<br>
</div>
<div class="inline-flex">
<input type="radio" name="smoker" [(ngModel)]="smoker"
[value]="false" [checked]="!smoker" /> No<br>
</div>
</fieldset>
<div class="full-width flex" *ngIf="smoker">
<fieldset class="one-quarter sm-padding">
<label>What do you smoke?</label>
<input list="whatDoYouSmoke" name="whatDoYouSmoke" tabindex="14">
<datalist id="whatDoYouSmoke">
<option value="Cigarettes">
<option value="Cigars">
<option value="Pipe">
<option value="E-Cigs">
<option value="Other">
</datalist>
</fieldset>
<fieldset class="one-quarter sm-padding">
<label>How many per day?</label>
<input type="number" min="0" name="howManySmokesPerDay"
tabindex="15">
</fieldset>
</div>

Clear form using angularjs

Problem: I am unable to clear the fields of the form completely.
Let me be more specific. When I go to the console, it appears that the previous entries are still present even though the reset button has been selected.
I have done the following in the form. I have a submit button and a reset button.
<div class="panel panel-default">
<div class="panel-body">
<form name="providerSearch" ng-submit="SearchProvider(searchParam);" novalidate role="form">
<div class="form-group"><input class="form-control" id="physiciansfirstname" ng-model="searchParam.FirstName" placeholder="First name:" type="text" /></div>
<div class="form-group"><input class="form-control" id="physicianslastname" ng-model="searchParam.LastName" placeholder="Last name:" type="text" /></div>
<!---<div class="form-group">
<select class="form-control" id="providerSpecialty" ng-model="searchParam.Specialty">
<option disabled="disabled" selected="selected" value="">Specialty</option>--->
<!---<option disabled="disabledvalue=""></option>---><!---<option>Family practice</option><option>General practice</option><option>Internal medicine</option><option>Pediatrics</option>
</select>--->
<div class="form-group">
<select class="form-control" id="providerSpecialty" ng-model="searchParam.Specialty">
<option selected="selected" value="">Specialty</option>
<!---<option value=""></option>--->
<cfoutput query="SpFind">
<option value=#ProviderSpecialty#>#ProviderSpecialty#</option>
</cfoutput>
</select>
</div>
<div class="form-group">
<select class="form-control" id="city" ng-model="searchParam.City">
<option selected="selected" value="">City</option>
<!---<option value=""></option>--->
<cfoutput query="cityFind">
<option value=#city#>#city#</option>
<!---<option ng-selected="{{searchParam.City==#city#?true:false}} value=#city#>#city#</option>--->
</cfoutput>
</select>
<!---<select class="form-control" id="city" ng-model="searchParam.City"><option disabled="disabled" selected="selected" value="">City</option><option ng-repeat="c in Cities" value="{{c.City}}">{{c.City}}</option> </select>--->
</div>
<div class="row">
<!---<div class="col-xs-6 no-right-padding paddingLanguage">
<div class="form-group widthLanguage">
<select id="language" name="language" class="form-control" ng-model="searchParam.Language">
<option disabled="disabled" selected="selected" value="">Language</option>
<!---<option value=""></option>--->
<cfoutput query="Languages">
<option value=#Language#>#Language#</option>
</cfoutput>
</select>
<!---<select name="language" class="form-control widthLanguage" id="language" ng-model="searchParam.Language">
<option disabled="disabled" selected="selected" value="">Language</option>
<option ng-repeat="l in Languages">{{l.Lang}}</option>
</select>--->
</div>
</div>--->
<div class="col-xs-6 no-left-padding">
<div class="form-group"><select class="form-control" id="gender" name="gender" ng-model="searchParam.Gender">
<option selected="selected" value="">Gender</option>
<!---<option value=""></option>--->
<option>Male</option><option>Female</option> </select></div>
</div>
</div>
<hr class="hrDoctor" />
<div style="margin-top:-10px; margin-bottom:10px; text-align:center; font-size:8pt! important">* or Search by Zip code radius *</div>
<div class="row">
<div class="col-xs-7 no-right-padding">
<div class="form-group">
<div class="input-group">
<select class="form-control" id="miles" name="distance" ng-model="searchParam.Distance">
<!---<option selected="selected" value="" disabled="disabled">Miles</option>--->
<option selected="disabled" value=""></option>
<option value={{v.value}} ng-repeat="(k , v) in miles track by $index">{{v.value}}</option>
<!---<option selected="disabled" value=""></option>
<option selected="selected" value="5">5</option>
<option selected="selected" value="10">10</option>
<option selected="selected" value="15">15</option>
<option selected="selected" value="20">20</option>--->
</select>
<div class="input-group-addon">mi</div>
</div>
</div>
</div>
<div class="col-xs-5 no-left-padding widthZip">
<div class="form-group">
<input allow-pattern="[\d\W]" class="form-control" id="zip" maxlength="5" ng-model="searchParam.Zip" placeholder="Zip code" type="text" />
</div>
</div>
</div>
<div class="form-group">
<input class="btn btn-warning btn-block" onclick="return checkTextField(); overlayDisplayButton();" ng-click="gotoElement('SearchResultsAnchor');" type="submit" value="Search" />
<div style="margin-top:10pt"><button type="reset" class="btn btn-info btn-block" ng-click="reset()">Reset</button></div>
</div>
</form>
</div>
Here is the following script to reset the form:
$scope.reset = function(form) {
form.$setPristine();
form.$setUntouched();
};
What am I missing to reset the form completely?
This should be your reset function:
$scope.reset = function() {
$scope.searchParam = {};
$scope.providerSearch.$setPristine();
$scope.providerSearch.$setUntouched();
};
where providerSearch is a form name.

AngularJS validation is not working

I'm new to AngularJS; can someone help me why the below code is not validating the form?
<form name="reviewForm" ng-controller="ReviewController as reviewCtrl" ng-submit="reviewCtrl.addReview(product);" novalidate>
<select ng-model="reviewCtrl.review.stars" required>
<option value="">Rate the Product</option>
<option value="1">1 Star</option>
<option value="2">2 Star</option>
</select>
<textarea ng-model="reviewCtrl.review.body"></textarea>
<input type="email" ng-model="reviewCtrl.review.author" required/>
<div>ReviewForm is: {{reviewForm.$valid}}</div>
<input type="submit" value="Submit" />
</form>
Remove novalidate from your form.
You have novalidate at the beginning of your form which is disabling your browser's native form validation.
Note that novalidate is used to disable browser's native form
validation.
See HTML 'form' novalidate Attribute and Angular Forms
At first step: Remove novalidate from your form
and add the submit button like this:
<input type="submit" value="Submit" ng-disabled="isFormValid(reviewForm) />
and in your .js:
$rootScope.isFormValid = function (form) {
return form.$invalid;
};
#Milton I think there is no problem with your code, i have created a plunkr that also shows that there is not any issue with your code.
Here is the code i have written.
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.min.js"></script>
<script>
function myctrl($scope) {
$scope.review = {};
}
</script>
</head>
<body>
<div>
<form name="reviewForm" ng-controller="myctrl as reviewCtrl" ng-submit="reviewCtrl.addReview(product);" novalidate>
<select ng-model="reviewCtrl.review.stars" required>
<option value="">Rate the Product</option>
<option value="1">1 Star</option>
<option value="2">2 Star</option>
</select>
<textarea ng-model="reviewCtrl.review.body"></textarea>
<input type="email" ng-model="reviewCtrl.review.author" required/>
<div>ReviewForm is: {{reviewForm.$valid}}</div>
<input type="submit" value="Submit" />
</form>
</div>
</body>
</html>
Here is the plunkr link of the code:
http://plnkr.co/edit/nVCmukG5abpi1Y4ZHkrq?p=preview
If the issue remains, can you give more description of data you are entering?

Skeuocard in Angular app

Did you guys have any luck setting up Skeuocard (http://kenkeiter.com/skeuocard/) in your AngularJS apps? I basically copied the example code, along with JS, installed it through bower and I am keep getting this:
TypeError: Cannot read property 'length' of undefined
at Skeuocard._importImplicitOptions (http://127.0.0.1:9000/lib/skeuocard/javascripts/skeuocard.min.js:1:4226)
at new Skeuocard (http://127.0.0.1:9000/lib/skeuocard/javascripts/skeuocard.min.js:1:1162)
at new controller (http://127.0.0.1:9000/js/modules/OtherDirectives.js:98:32)
at invoke (http://127.0.0.1:9000/lib/angular/angular.js:3760:17)
at Object.instantiate (http://127.0.0.1:9000/lib/angular/angular.js:3771:23)
at http://127.0.0.1:9000/lib/angular/angular.js:6881:28
at http://127.0.0.1:9000/lib/angular/angular.js:6268:34
at forEach (http://127.0.0.1:9000/lib/angular/angular.js:329:20)
at nodeLinkFn (http://127.0.0.1:9000/lib/angular/angular.js:6255:11)
at http://127.0.0.1:9000/lib/angular/angular.js:6529:13
My init code looks like this:
var card = new Skeuocard($("#skcard"));
My template:
<div id="skcard" class="credit-card-input no-js margin_auto">
<p class="no-support-warning">Either you have Javascript disabled, or you're using an unsupported browser, amigo! That's why you're seeing this old-school credit card input form instead of a fancy new Skeuocard. On the other hand, at least you know it gracefully degrades...
<label for="cc_type">Card Type</label>
<select name="cc_type">
<option value="">...</option>
<option value="visa">Visa</option>
<option value="discover">Discover</option>
<option value="mastercard">MasterCard</option>
<option value="maestro">Maestro</option>
<option value="jcb">JCB</option>
<option value="unionpay">China UnionPay</option>
<option value="amex">American Express</option>
<option value="dinersclubintl">Diners Club</option>
</select>
<label for="cc_number">Card Number</label>
<input id="cc_number" type="text" name="cc_number" placeholder="XXXX XXXX XXXX XXXX" maxlength="19" size="19"/>
<label for="cc_exp_month">Expiration Month</label>
<input id="cc_exp_month" type="text" name="cc_exp_month" placeholder="00"/>
<label for="cc_exp_year">Expiration Year</label>
<input id="cc_exp_year" type="text" name="cc_exp_year" placeholder="00"/>
<label for="cc_name">Cardholder's Name</label>
<input id="cc_name" type="text" name="cc_name" placeholder="John Doe"/>
<label for="cc_cvc">Card Validation Code</label>
<input id="cc_cvc" type="text" name="cc_cvc" placeholder="123" maxlength="3" size="3"/>
</p>
</div>
Any ideas for this?
Thanks
After 3 days of debugging, trying, and beating my head against the wall, I have fount the problem. The bloody HTML2JADE converted converted the whole thing into paragraph, that's why it wasn' working... with this code, it works:
<div id="skeuocard" class="credit-card-input no-js margin_auto">
<p class="no-support-warning">Either you have Javascript disabled, or you're using an unsupported browser, amigo! That's why you're seeing this old-school credit card input form instead of a fancy new Skeuocard. On the other hand, at least you know it gracefully degrades...</p>
<label for="cc_type">Card Type</label>
<select name="cc_type">
<option value="">...</option>
<option value="visa">Visa</option>
<option value="discover">Discover</option>
<option value="mastercard">MasterCard</option>
<option value="maestro">Maestro</option>
<option value="jcb">JCB</option>
<option value="unionpay">China UnionPay</option>
<option value="amex">American Express</option>
<option value="dinersclubintl">Diners Club</option>
</select>
<label for="cc_number">Card Number</label>
<input id="cc_number" type="text" name="cc_number" placeholder="XXXX XXXX XXXX XXXX" maxlength="19" size="19"/>
<label for="cc_exp_month">Expiration Month</label>
<input id="cc_exp_month" type="text" name="cc_exp_month" placeholder="00"/>
<label for="cc_exp_year">Expiration Year</label>
<input id="cc_exp_year" type="text" name="cc_exp_year" placeholder="00"/>
<label for="cc_name">Cardholder's Name</label>
<input id="cc_name" type="text" name="cc_name" placeholder="John Doe"/>
<label for="cc_cvc">Card Validation Code</label>
<input id="cc_cvc" type="text" name="cc_cvc" placeholder="123" maxlength="3" size="3"/>
</div>

Angular js form submit

I am new to Angularjs.
Currently i have code like this that submits the form on button click
<form ng-submit="reloadA(resultsPerPage)">
<select ng-model="resultsPerPage">
<option value="10">10</option>
<option value="25">25</option>
</select>
<input type="submit" id="submit" value="Submit" />
</form>
I need that form should be submitted on selecting the option without need of the submit button
ok, for this you should have that ng-submit function in the select, like this;
<form>
<select ng-change="reloadA(resultsPerPage)" ng-model="resultsPerPage">
<option value="10">10</option>
<option value="25">25</option>
</select>
<input type="submit" id="submit" value="Submit" />
</form>
I believe you can use ng-change here
<select ng-model="resultsPerPage" ng-change="reloadA(resultsPerPage)">

Resources