I have this HTML template:
<div class="container">
<div class="row">
<div class="col-md-12">
<div data-role="dynamic-fields">
<div class="form-inline">
<div class="form-group">
<label class="sr-only" for="field-name">Link </label>
<input type="text" class="form-control" id="field-name" placeholder="Link"
[(ngModel)]="HrefLink" name="HrefLink">
</div>
<span>-</span>
<div class="form-group">
<label class="sr-only" for="field-value">Label</label>
<input type="text" class="form-control" id="Label" placeholder="Label"
[(ngModel)]="HrefLabel" name="HrefLabel">
</div>
<button class="btn btn-danger" data-role="remove">
<span class="glyphicon glyphicon-remove"></span>
</button>
<button class="btn btn-primary" data-role="add">
<span class="glyphicon glyphicon-plus"></span>
</button>
</div> <!-- /div.form-inline -->
</div> <!-- /div[data-role="dynamic-fields"] -->
</div> <!-- /div.col-md-12 -->
</div> <!-- /div.row -->
</div>
In this simple form, I can add one or more inputs. This is the live example: https://bootsnipp.com/snippets/VPRlZ
My question is: How can I get the values of all added fields inside my angular component ? I can add the directive ngModel like above in every input but then all directives will have the same name and I'll not get all values?
Unfortunately, I can't place the ngModel in a higher level in the div of form-group ...
Please tell me if my question is clear or you need more information.
Why not you base the quantity of input in an array of Objects(InputModel it is a custom interface or class) and finally use *ngfor to show them.
InputModel
{
input_value: string,
index_of: number,
isDelete: boolean, //if false is "+" if true is "x"
}
Component.ts
inputValues: InputModel[] = [];
count: number = 0;
OnInit{
this.inputValues.push({
input_value:"",
index_of: this.count,
isDelete: true
});
}
addMore(){
this.inputValues.push({
input_value:"",
index_of: this.count++,
isDelete: false
});
}
Html Template
<div *ngFor="let input of inputValues">
<input [(ngModel)]="input.input_value" />
<!-- TODO button with 'x' or '+' depends of input.isDelete -->
</div>
Related
My code is as given below:
export class SearchByParametersComponent implements OnInit {
parametersForm=new FormGroup({
NumAppForm:new FormControl (''),
UniqeIDForm :new FormControl (''),
StartDateForm :new FormControl (''),
EndDateForm :new FormControl (''),
NumStatusForm :new FormControl (''),
});
showAll()
{
//x:string
debugger;
this.NumApp=this.parametersForm.controls.NumAppForm.value;
this.x=this.parametersForm.controls.UniqeIDForm.value;
this.NumStatus=this.parametersForm.controls.NumStatusForm.value;
this.sd=this.parametersForm.controls.StartDateForm.value;
// this.sd= this.parametersForm.get('StartDateForm').value;
// this.ed=this.parametersForm.controls.EndDateForm.value;
console.log(this.parametersForm);
if (this.NumApp==undefined) {
alert("חובה לבחור מספר מערכת");
}
else
{
if (this.x!="" && this.x!=undefined)
{
this.ParametersToSearch.NumApp=this.NumApp;
debugger;
this.ParametersToSearch.UniqeID=String(this.x);
debugger
this.checkUniqeId.emit(this.ParametersToSearch);
debugger;
this.ParametersToSearch=new ParametersToSearchDTO();
debugger;
//this.checkUniqeId.emit(x);
}
else if (this.StartDate != null && this.EndDate != null)
{
this.DateObject.StartDate=String(this.StartDate["day"]+"-"+this.StartDate["month"]+"-"+this.StartDate["year"]);
this.DateObject.EndDate=String(this.EndDate["day"]+"-"+this.EndDate["month"]+"-"+this.EndDate["year"]);
this.DateObject.NumApp=this.NumApp;
debugger;
this.checkRangeDate.emit(this.DateObject);
this.DateObject=new DateRangeDTO();
debugger;
}
else if(this.NumStatus!="" && this.NumStatus!=undefined)
{
debugger;
this.StatusSearch.NumApp=this.NumApp;
this.StatusSearch.NumStatus=this.NumStatus;
this.checkStatus.emit(this.StatusSearch);
this.StatusSearch=new StatusSearchDTO();
debugger;
}
else
{
debugger;
this.checkNumApp.emit(this.NumApp);
debugger;
}
}
<form [formGroup]="parametersForm">
<div class="myPage">
<div class="form-group title1">
<span> קוד מערכת <span class="must" title="שדה חובה">*</span></span>
</div>
<div class="form-group title2">
<select class="form-control" formControlName="NumAppForm"
(change)="onChangeApp($event.target.value)" required>
<option>----------</option>
<option *ngFor="let index of ListApp">{{index}}</option>
</select>
</div>
<div class="form-group title3">
<span dir="rtl"> UniqeId </span>
</div>
<div class="form-group title4">
<input class="form-control" formControlName="UniqeIDForm" type="text">
</div>
<div class="form-group title5">
<span class="line"> צרוף קובץ מ</span>
</div>
<div class="form-group title6">
<app-date-picker formControlName="StartDateForm" (dateSelect)="dateSelectStart($event)"></app-date-picker>
</div>
<div class="form-group title7">
<span class="line"> עד</span>
</div>
<div class="form-group title8">
<app-date-picker formControlName="EndDateForm" (dateSelect)="dateSelectEnd($event)" ></app-date-picker>
</div>
<div class="form-group title9">
<span> סטטוס</span>
</div>
<div class="form-group title10">
<select class="form-control" formControlName="NumStatusForm" (change)="onChangeStatus($event.target.value)">
<option>----------</option>
<option *ngFor="let index of ListStatus">{{index}}</option>
</select>
</div>
</div>
</form>
<button type="submit" dir="ltr" class="btn btn-primary" (click)="showAll(); reset()">אישור</button>
<!-- <input class="radio" type="checkbox" title="הכנס UniqeId" label="הכנס" /> <br> -->
<!-- [(ngModel)]="NewUniqeId.value" -->
<!-- formControlName="StartDateForm" -->
<!-- formControlName="EndDateForm" -->
<!-- [formControl]="parametersForm.controls.StartDateForm" -->
a project on Angular 8:
I have a DATE-PICKER tag in HTML
And I have FORM-CONTROL that contains more tags like:
INPUT, SELECT ...
All the FORM-CONTROL elements are identified
By the FROM-CONTROL-NAME attribute
But only the DATE-PICKER element is unknown at all and makes an error when trying to set the FROM-CONTROL-NAME property to it
what can we do?
I need to know it in FORM so I can clear all content with elements with one click .....
The app-date-picker is not a form control and as such cannot set the formControlName. The component should implement ControlValueAccessor to work as a form control. If you need to build custom form controls, please refer
Angular 2 - formControlName inside component
I am having an issue with my HTML component, for some reason the my method is displaying an error:
Cannot read property 'setValue' of undefined when I click the edit button, however everything seems to be ok.
This is my HTML
<div *ngFor="let item of currentUser.myExperiences" class="mt-3">
<div>
<h6>{{item.companyName}} <span *ngIf="item.currentlyWorking" class="badge badge-warning">Currently
working</span>
<span class="float-right">
<a class="text-warning cursor" (click)="openEditModal(item)"><i class="fas fa-pencil-alt"></i></a>
</span>
</h6>
<p class="text-muted p-text-sm">{{item.functions}}</p>
<p class="text-muted p-text-sm" *ngIf="item.references">
<strong>References:</strong>
{{item.references}}
</p>
<hr>
</div>
</div>
<form role="form" [formGroup]="updateForm">
<div class="form-group">
<label for="functions">Funtions</label>
<input type="text" class="form-control" id="functions" name="functions" formControlName="functions" aria-describedby="emailHelp">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
This is my TS component:
public openEditModal(myExperience: IMyExperience): void {
console.log(myExperience);
// Set values to controls
console.log(myExperience.id);
this.experienceId.setValue(myExperience.id);
this.companyName.setValue(myExperience.companyName);
this.functions.setValue(myExperience.functions);
this.workedYears.setValue(myExperience.workedYears);
this.references.setValue(myExperience.references);
this.currentlyWorking.setValue(myExperience.currentlyWorking);
this.updateForm.setValue({
experienceId: this.experienceId.value,
companyName: this.companyName.value,
functions: this.functions.value,
workedYears: this.workedYears.value,
references: this.references.value,
currentlyWorking: this.currentlyWorking.value
});
// Open the modal
this.modalRef = this.modalService.show(
this.editModal,
Object.assign({}, { class: 'gray modal-lg' })
);}
I tried displaying in console the object, but it works perfectly, however I am not sure why this is happening.
I use resolvers to pass information from the route to the component properly.
Thanks in advance.
Try to use this.updateForm.patchValue instead of this.updateForm.setValue.
i'm using Angular, express (node).
I am opening a modal after validating the user's data at login (httpClient - express).
But when I open the modal (yes, the modal opens) I get an exception in the console:
ExpressionChangedAfterItHasBeenCheckedError:
Expression has changed after it was checked. Previous value:
'ng-untouched: true'. Current value: 'ng-untouched: false'.
Here is my basic code:
example.html
<ng-template #modalexample let-c="close" let-d="dismiss">
/** modal body **/
</ng-template>
form (ngSubmit)="f.form.valid && signin()" #f="ngForm" class="m-login__form m-form" action="">
<ng-template #alertSignin></ng-template>
<div class="form-group m-form__group">
<input class="form-control m-input" type="text" placeholder="Email" name="email" [(ngModel)]="model.email" #email="ngModel" autocomplete="off">
</div>
<div class="form-group m-form__group">
<input class="form-control m-input m-login__form-input--last" type="password" placeholder="Password" name="password" [(ngModel)]="model.password" #password="ngModel">
</div>
<div class="row m-login__form-sub">
<div class="col m--align-left">
<label class="m-checkbox m-checkbox--focus">
<input type="checkbox" name="remember" [(ngModel)]="model.remember" #remember="ngModel">
Remember me
<span></span>
</label>
</div>
<div class="col m--align-right">
<a href="javascript:;" id="m_login_forget_password" class="m-link">
Forget Password ?
</a>
</div>
</div>
<div class="m-login__form-action">
<button [disabled]="loading" [ngClass]="{'m-loader m-loader--right m-loader--light': loading}" id="m_login_signin_submit" class="btn btn-focus m-btn m-btn--pill m-btn--custom m-btn--air">
Sign In
</button>
</div>
</form>
And my component.ts
#ViewChild('modalexample', {read: TemplateRef}) modal: TemplateRef<any>;
signin() {
console.log("Signin")
this.modalService.open(this.content);
this.loading=true;
this._authUser.login(this.model.email, this.model.password).subscribe(
response=>{
if(!response.error){
this.loading=false;
}
else{
alert('Error');
}
this.loading=false;
},
error=>{
console.log(error)
this.loading=false;
}
)
}
How can I solve it?
Thank you :D
I have a form that is little tricky and I'm hoping to submit it with multi-dimensional array. The form is divided as follows.
The first input is lets say a meal name input like breakfast, lunch or dinner. After it enters it. and click on the button add detail.
A new input field will appear called meal-content. After adding it he clicks the button attached to content. A new input will appear called calories. So I Want to submit it like this.
Meal Name->Content->Calories.
There are button like add meal which will add another meal field.
Add Meal Content which will add another meal content for same meal.
So when I submit it the posted data is like this.
Breakfast->Egg->200
->Juice->150
Lunch->Pasta->250
->Spaghetti->190
The problem here is I cannot use index like 0 for names like
Meal[0][Calories][Content]
Because fields are generated at runtime so i cannot know which meal has how many contents and how many meals are there.
<form action="{{route('add-diet')}}" class="diet-program form-horizontal bordered-row" method="post">
{{csrf_field()}}
<div class="tab-content">
<h3 class="content-box-header bg-default">Diet Program Form</h3>
<div class="form-group first-group">
<label class="col-sm-3 control-label">Program Name</label>
<div class="col-sm-6">
<input class="form-control" name="name" placeholder="Program Name... (Optional)" type="text" value="">
</div>
</div>
<div class="form-group meal_number">
<label class="col-sm-3 control-label">Meal Number</label>
<div class="col-sm-6">
<div class="input-group">
<input class="form-control meal_number_input" name="[number][]" placeholder="Meal Number..." type="text" value=""> <span class="input-group-btn"><button class="btn btn-blue-alt meal_detail" disabled type="button"><span class="input-group-btn"><span class="input-group-btn">Add Details</span></span></button></span>
</div>
<div aria-hidden="true" class="modal fade meal_details_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button aria-hidden="true" class="close" data-dismiss="modal" type="button">×</button>
<h4 class="modal-title">Meal Content</h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="meal_container">
<div class="meal_content-div col-md-12 col-sm-12 col-xs-12">
<div class="form-group input-group meal_name-div">
<input class="form-control meal_name_input" name="content[]" placeholder="Meal Content..." type="text" value=""> <span class="input-group-btn"><button class="btn btn-blue-alt meal_name_detail" disabled type="button"><span class="input-group-btn"><span>Add Details</span> <i class="glyph-icon icon-plus"></i></span></button></span>
</div>
<div class="form-group meal_name_detail-div">
<div class="col-md-10 col-md-offset-1 form-group calories-div">
<input class="form-control calories" name="calories[]" placeholder="Calories..." type="number" value="">
</div>
<div class="col-md-10 col-md-offset-1 form-group time_taken-div">
<input class="form-control time_take_input" name="taketime[]" placeholder="Time to take... (Optional)" readonly type="text">
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-6">
<button class="btn btn-alt btn-hover btn-primary add_meal_name" type="button"><span>Add Content</span><i class="glyph-icon icon-arrow-up"></i></button>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-default" data-dismiss="modal" type="button">Done</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-6 col-md-offset-3">
<button class="btn btn-alt btn-hover btn-primary add_meal_number" type="button"><span>Add Meal</span> <i class="glyph-icon icon-arrow-up"></i></button>
</div>
</div>
</div>
<div class="form-group submit-btn-div">
<button class="btn ra-100 btn-default" type="submit">Submit</button>
</div>
</form>
My Javascript
/***
* Cloned Div for future duplication
*/
var cloned_meal_number = $('.meal_number').clone();
var meal_content = $('.meal_content-div').clone();
/***
* Enable Meal Number or Meal Name If Entered
*/
$('.tab-content').on('input','.meal_number_input, .meal_name_input',function () {
if($(this).val()) {
$(this).siblings('.input-group-btn').children().prop('disabled',false);
}
else{
$(this).siblings('.input-group-btn').children().prop('disabled',true);
}
});
/***
* Meal Detail Modal Load
*/
$('.tab-content').on('click','.meal_detail',function () {
var meal_number_val = $(this).parent().siblings('.meal_number_input').val();
$(this).closest('.input-group').siblings('.modal').find('.modal-title').html(meal_number_val);
$(this).closest('.input-group').siblings('.meal_details_modal').modal({
keyboard: 'false',
backdrop: 'static'
})
});
/***
* Modal Hide
*/
$('.tab-content').on('click','.modal_close, .close',function () {
$(this).closest('.meal_details_modal').modal('hide');
});
/***
* Meal Detail Toggle Menu
*/
$('.tab-content').on('click','.meal_name_detail', function () {
$(this).find('i').toggleClass('icon-plus icon-minus');
$(this).closest('.meal_name-div').siblings('.meal_name_detail-div').slideToggle();
});
/***
* Add Meal Number
*/
$('.tab-content').on('click','.add_meal_number',function () {
cloned_meal_number.clone().insertAfter('.meal_number:last');
});
/***
* Add Day Button
*/
$('.tab-content').on('click','.add_meal_name',function () {
meal_content.clone().appendTo($('.meal_container'));
});
you can use foreach loop within a foreach loop to run all over your array. I would've used an array of objects for this task since its far more easier to parse than multidimensional arrays, they are also very flexible and will save you precious time, one dimension also, consider that.
as for code example:
foreach($mainArray as $childArray=>$someArrayProp){ }
You can create a JSON with all the data you need like this:
[{
"meal": "breakfast",
"content": [{
"name": "egg",
"calories": "200"
}, {
"name": "juice",
"calories": "150"
}
]
}, {
"meal": "lunch",
"content": [{
"name": "pasta",
"calories": "250"
}, {
"name": "spaghetti",
"calories": "190"
}
]
}
]
but I'm not sure if this is what you need, tell me if not!
Michele
I am trying to disable the save button untill date is not picked. it is disabled but its not able to enbale untill i dont press any key from the keyboard.please tell me what i am doing wrong and thanks in advance.
<div class = "form-group" ng-class="{ 'has-error' : Form.fromTime.$invalid && !Form.fromTime.$pristine }">
<label for = "fromTime"> From Time:
<img src = "images/required.gif" alt = "Required" class = "required-star">
</label>
<div class='input-group date' id='fromTime' >
<input type='text' class="form-control" name="fromTime" ng-model = "fromTime" required />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<p ng-show="Form.fromTime.$invalid && !Form.fromTime.$pristine" class="help-block">From Time is required.</p>
</div>
<button type="button" class="col-sm-2 btn btn-primary" ng-click="scheduleCall()" style="margin-left:10px;" ng-disabled="Form.$invalid"> Save </button>
change:
ng-disabled="Form.$invalid"
to:
ng-disabled="Form.fromTime.$invalid"
You have to use like below
ng-disabled="Form.fromTime.$invalid"
Formname: Form;
Input field name : fromTime;
Input field state: $invalid
If i understand your problem correctly, you need to use a date input.
Now save button will be disabled until a valid date is picked.
function Ctrl($scope) {
$scope.fromTime = '';
$scope.scheduleCall = function() {};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<div ng-app>
<div ng-controller="Ctrl">
<form name="Form">
<div class="form-group"
ng-class="{ 'has-error' : Form.fromTime.$invalid && !Form.fromTime.$pristine }">
<label for="fromTime">From Time:</label>
<div class='input-group date' id='fromTime'>
<input type='date' class="form-control" name="fromTime" ng-model="fromTime" required />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<p ng-show="Form.fromTime.$invalid && !Form.fromTime.$pristine"
style="font-size: 11px; color: red;">
From Time is required.
</p>
</div>
<button type="button" ng-click="scheduleCall()" ng-disabled="Form.$invalid">Save</button>
</form>
</div>
</div>