angular-forms

Cannot find control with name: formControlName in angular 2 or 4

[亡魂溺海] 提交于 2019-11-30 04:43:19
I found this problem in many questions in stackoverflow but no luck. Please help me for figuring out what I am doing wrong. In component : ngOnInit() { this.companyCreatForm = this._formBuilder.group({ name: [null, [Validators.required, Validators.minLength(5)]], about: [null, [Validators.required]], businessType: [null, [Validators.required]], address: this._formBuilder.group({ street: [], website: [null, [Validators.required]], mobile: [null, [Validators.required]], email: [null, [Validators.required]], pageId: [null, [Validators.required]], }), }); Form : <form [formGroup]="companyCreatForm

How to access multiple checkbox values in Angular 4/5

不羁的心 提交于 2019-11-30 04:13:51
问题 I want to get the values from my checkboxes but I can only get true or false. Here is my template: <h4>Categories</h4> <div class="form-check" *ngFor="let cat of categories$|async"> <input class="form-check-input" [(ngModel)]="cat.id" name="{{ cat.name }}" type="checkbox" id="{{cat.name}}"> <label class="form-check-label" for="{{cat.name}}"> {{cat.name}} </label> </div> Here is my component this.categories$ = this.storeService.getAllCategories().pipe( map(result => (result as any).data), tap

How do I know when custom form control is marked as pristine in Angular?

拟墨画扇 提交于 2019-11-29 14:20:39
I have several custom form control components in my Angular application, which implement ControlValueAccessor interface and it works great. However, when markAsPristine() is called on parent form, or on my custom control directly I need to update it's state: my custom control is actually have internal control and I need to call markAsPristine() on it too. SO, how do I know when markAsPristine() is called on my control? The ControlValueAccessor interface has no members, related to this problem, which I can implement. After thorough investigation I've found out that this functionality is not

Angular 2: Apply Validator.required validation on some condition

折月煮酒 提交于 2019-11-29 02:27:58
I have a angular 2 form wherein I have to make a field required on some condition like: description: ['', Validators.required] This description field will be required only on some type of a condition like: if(true){descReq = true}; How can I achieve this, please suggest. Thanks in advance! You can add or remove a validator based on the the value of another control on the form: testForm: FormGroup; constructor(private formBuilder: FormBuilder) { this.testForm = this.formBuilder.group({ condition: [''], description: [''] }); this.testForm.controls['condition'].valueChanges.subscribe(result => {

Angular2 Reactive Forms formControl for radio buttons

[亡魂溺海] 提交于 2019-11-29 00:58:49
I'm building a form in Angular with Reactive Forms. I'm using the FormBuilder to make a group of fields. For textboxes this works extremely well. But I can't find a formControl for radio buttons. How does this work? Should I use <input formControlName="gender" type="radio"> just like I use with text input's? Should I do <input formControlName="gender" type="radio"> just like I do with text input's? Yes. How does this work? Form control directives use a ControlValueAccessor directive to communicate with the native element. There are different types of ControlValueAccessors for each of the

Angular 2: Form submission canceled because the form is not connected

依然范特西╮ 提交于 2019-11-28 19:03:51
I have a modal that contains a form, when the modal is destroyed I get the following error in the console: Form submission canceled because the form is not connected The modal is added to a <modal-placeholder> element which is a direct child to <app-root> , my top level element. What's the correct way to removing a form from the DOM and getting rid of this error in Angular 2? I currently use componentRef.destroy(); There might be other reasons this occurs but in my case I had a button that was interpreted by the browser as a submit button and hence the form was submitted when the button was

Enable When Checkbox is Check in Reactive Forms

旧城冷巷雨未停 提交于 2019-11-28 14:18:16
I need help in making the rows enable only when the checkbox is check. The default rows should be disabled but when the checkbox is only check, that row will be enabled. Here's the link to my code LINK CODES patchValues() { let rows = this.myForm.get('rows') as FormArray; this.orders.forEach(material => { material.materials.forEach(x => { rows.push(this.fb.group({ checkbox_value: [null], material_id: new FormControl({value: x.id, disabled: true}, Validators.required), material_name: x.name, quantity: [null, Validators.required] })) }) }) } Have a look at the Demo & Src Stack Blitz, Fork

Limit input field to two decimal places - Angular 5

为君一笑 提交于 2019-11-28 09:27:46
The code is as follows <input type="number" class="form-control" value="" name="cost_price" #name="ngModel" [(ngModel)]="item.cost_price" placeholder="Cost Price" /> User should not be able to type more that 2 decimal places. For example, if the user wants to enter 21.256. He should be only allowed to enter 21.25 How to achieve this using angular 5? First create Directive for limit the two decimal places in typescript like this: import { Directive, ElementRef, HostListener } from '@angular/core'; @Directive({ selector: '[appTwoDigitDecimaNumber]' }) export class TwoDigitDecimaNumberDirective {

Can't bind to 'formControl' since it isn't a known property of 'input' - angular2 material Autocomplete issue

若如初见. 提交于 2019-11-28 03:43:36
I am trying to use angular material autocomplete component in my angular2 project. I added following to my template. <md-input-container> <input mdInput placeholder="Category" [mdAutocomplete]="auto" [formControl]="stateCtrl"> </md-input-container> <md-autocomplete #auto="mdAutocomplete"> <md-option *ngFor="let state of filteredStates | async" [value]="state"> {{ state }} </md-option> </md-autocomplete> Following is my component. import {Component, OnInit} from "@angular/core"; import {ActivatedRoute, Router} from "@angular/router"; import {FormControl} from "@angular/forms"; @Component({

Detect if data in form was changed

青春壹個敷衍的年華 提交于 2019-11-28 01:51:50
I have Angular form (not reactive), with data binded in ngModel: <form #form="ngForm"> <input [(ngModel)]="user.name"> <input [(ngModel)]="user.color"> <button type="submit">Save</button> </form> How can i disable submit button if binded data has not been changed? do this , check for dirty flag, which tells form dirty or not <button type="submit" [disabled]="!form.dirty">Save</button> form becomes dirty if you change some value in it. Check here for detail : https://angular.io/guide/forms According to your comment 'But what if i erase 1 symbol in input and then wright it again (the value is