angular-forms

Angular NgForm: reset exact form filed value does not make it valid

懵懂的女人 提交于 2019-12-05 12:50:21
I have a form on a component's template: <form (ngSubmit)="submitLogin(loginForm)" #loginForm="ngForm"> <mat-input-container> <input matInput [placeholder]="'User Name'" ngModel name="username" autofocus required> </mat-input-container> <br> <mat-input-container> <input matInput [placeholder]="'Password'" ngModel type="password" name="password" required> </mat-input-container> <br> <button [disabled]="loginForm.invalid" mat-raised-button type="submit"> Login </button> </form> And here is my component's submit handler: public submitLogin(loginForm: NgForm) { return this.authService.login

How to allow nested components to be tracked by their parent and get values from their parent in Angular?

China☆狼群 提交于 2019-12-04 12:15:21
I have a series of forms (each managed by 1 component). There is a pattern of inputs in these forms (e.g. many of them require to allow input of an address) that I have to refactor into re-usable components as they are used in multiple forms and I don't want to duplicate neither their logic nor their templates. Each re-usable component would have to have its logic have its template containing input tags and no <form> tag have its client validation constraints possibly receive initial values from its parent be able to return the value of its fields to the parent as an object (e.g. address:

Date and Currency validation in Angular (4)

与世无争的帅哥 提交于 2019-12-04 00:51:42
问题 I am new to Angular. I am using angular 4 reactive forms and figured out how to perform custom validations. Following is my implementation for numeric function numberValidator(c: AbstractControl): { [key: string]: boolean } | null { if (c.pristine) { return null; } if (c.value.match(/.*[^0-9].*/)) { return { 'numeric': true }; } return null; } phoneControl: ['', [Validators.required, Validators.minLength(10), Validators.maxLength(10), numberValidator]], I am trying to find out how to perform

How to change the date format in the datepicker of Angular ngx-bootstrap inside a form

▼魔方 西西 提交于 2019-12-03 17:47:39
问题 Using ngx-bootstrap Datepicker in an Angular 4 application, I know that normally you can specify the date format this way: <input id="from" name="from" bsDatepicker [(bsValue)]="myModel" value="{{ myModel | date:'yyyy-MM-dd'}}"> but this time I'm inside a template-driven Angular form, so my input is not bound to a variable like myModel in the example above, but it's just bound to the form: <input id="from" name="from" bsDatepicker ngModel> so how can I specify the date format in this case?

Angular 4 Dynamic Forms: Dependent Dropdown

拜拜、爱过 提交于 2019-12-03 17:30:29
I am creating a dynamic form based on Angular 4 Dynamic Forms . Everything is working out great! However, I have run into an issue with the dropdown. I would like to have a dependent dropdown. When the user selects a value in the dropdown it will display checkboxes, based on an attribute - possibly name . service new DropdownInput({ key: 'dropdown', label: 'Dropdown Testing', options: [ {key: 'example1', value: 'Example 1'}, {key: 'example2', value: 'Example 2'} ], order: 1 }), new CheckboxInput({ key: 'checkbox1', label: 'checkbox1 - example1', name: 'example1', order: 2 }), new CheckboxInput

Angular form updateValueAndValidity of all children controls

青春壹個敷衍的年華 提交于 2019-12-03 16:25:16
问题 In my Angular 4 app, I have a form with several controls. At some points I need to force the update of their validity, so I'm doing: this.form.get('control1').updateValueAndValidity(); this.form.get('control2').updateValueAndValidity(); this.form.get('control3').updateValueAndValidity(); // and so on.... and then: this.form.updateValueAndValidity(); this works fine. However I was wondering if there is a better way to accomplish the same thing, by just calling one method on the parent form.

Error during dynamic form creation from json Cannot find control with name: 'data'

筅森魡賤 提交于 2019-12-03 16:19:12
I am using angular 7 appplication and i am writing a generic dynamic form creator. I am using reactive forms and ng-template and <ng-container *ngTemplateOutlet> for recursively displaying the form element.The code of the project could be seen Here . But i am facing the following error ERROR Error: Cannot find control with name: 'data' at _throwError (forms.js:1775) at setUpControl (forms.js:1683) at FormGroupDirective.push../node_modules/@angular/forms/fesm5/forms.js.FormGroupDirective.addControl (forms.js:4532) at FormControlName.push../node_modules/@angular/forms/fesm5/forms.js

Form in form. Can there be inheritance of form controls?

痞子三分冷 提交于 2019-12-03 14:59:20
I have two components: ParentComponent and ChildComponent: parent.component.ts <form #form="ngForm" (ngSubmit)="onSubmit(form)" novalidate> <input type="text" name="firstControl" [(ngModel)]="firstControl" /> <input type="text" name="secondControl" [(ngModel)]="secondControl" /> <child-component> </form> {{form.value | json}} child.component.ts <input type="text" name="thirdControl" [(ngModel)]="thirdControl" /> <input type="text" name="fourthControl" [(ngModel)]="fourthControl" /> Now, {{form.value | json}} returns { "firstControl": "", "secondControl": "" } and it's clear. My question is: Is

How to change the date format in the datepicker of Angular ngx-bootstrap inside a form

那年仲夏 提交于 2019-12-03 13:22:52
Using ngx-bootstrap Datepicker in an Angular 4 application, I know that normally you can specify the date format this way: <input id="from" name="from" bsDatepicker [(bsValue)]="myModel" value="{{ myModel | date:'yyyy-MM-dd'}}"> but this time I'm inside a template-driven Angular form, so my input is not bound to a variable like myModel in the example above, but it's just bound to the form: <input id="from" name="from" bsDatepicker ngModel> so how can I specify the date format in this case? Edit : it looks like that a way to achieve this is by creating a new variable newVar inside the component

Set focus on <input> element

↘锁芯ラ 提交于 2019-12-03 10:27:26
问题 I am working a front-end application with Angular 5, and I need to have a search box hidden, but on click of a button, the search box should be displayed and focused. I have tried a few ways found on StackOverflow with directive or so, but can't succeed. Here is the sample code: @Component({ selector: 'my-app', template: ` <div> <h2>Hello</h2> </div> <button (click) ="showSearch()">Show Search</button> <p></p> <form> <div > <input *ngIf="show" #search type="text" /> </div> </form> `, })