angular-forms

How to return object from service to component in angular

孤街浪徒 提交于 2019-12-20 06:39:53
问题 I want to access particular json in ngOninit. So i have id on click in edit button and with the help of that id i am getting complete object from database like form name , form json which etc. So from the service i want to return that json to ngOninit. Here is service. GetFormById (id: number) { return this.httpClient.get<FormTemplate[]>(this.API_URL + "GetFormTemplate/" + id).subscribe(data => { console.log(data); return data; }); } In console i am getting complete object from database which

Custom Validator Control Quantity in Reactive Forms

对着背影说爱祢 提交于 2019-12-20 05:43:09
问题 I had a hard time implementing a custom validation in my reactive forms in Angular. I need to control the quantity. The quantity should not be more than the available quantity. The problem how can i get the total of all the quantity if each row has subrows. How will i able to compute the total of subrows and compare it to its parent row where the available quantity is found. Here's my code below. Here's also the link to my code PLEASE CLICK THIS LINK customValidator(campo1: string) { return

angular 5 populate form fields using other field values if checkbox is selected

我怕爱的太早我们不能终老 提交于 2019-12-20 04:38:38
问题 I've got two sections of forms - correspondence address and residential address. The goal is to copy/pass values from one field to another if a checkbox is checked. Kindly see below use case populate the correspondence address fields with the values of the residential address form fields iff a checkbox (same as residential address) is checked Should any form field in the residential address change after checking the checkbox, pass the change(s) realtime to the field in the correspondence

Angular 6 FormGroup.disable() method is not working with my template driven NgForm

白昼怎懂夜的黑 提交于 2019-12-20 00:23:51
问题 When I try to use the disable method on a formGroup in my Angular 6 app , I get this error in the browser console : TypeError: this.personForm.disable is not a function Although the method is mentioned in the documentation & it is even suggested by VS Code as in this snapshot. My code is here: // ... many parts skipped, the form here is template driven // but still personForm is a FormGroup , // I am trying to disable the whole FormGroup with all child elements @ViewChild('personForm')

Nested Headers and Table in Reactive Forms

我是研究僧i 提交于 2019-12-18 09:37:39
问题 I have several categories and each category has several materials/products. I have a problem since when i decided to delete a material/product on a category, I also delete a material/product in another category? How would able to fix this using reactive forms? How would i independently delete one material only and not another? This is the link to my code CODE LINK patchValues() { let rows = this.myForm.get('rows') as FormArray; this.orders.forEach(material => { material.materials.forEach(x =>

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

冷暖自知 提交于 2019-12-18 07:45:20
问题 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

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

微笑、不失礼 提交于 2019-12-18 07:44:29
问题 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

Angular 2: Apply Validator.required validation on some condition

好久不见. 提交于 2019-12-18 03:42:23
问题 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! 回答1: 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({

How to use Angular4 to set focus by element id

巧了我就是萌 提交于 2019-12-18 01:52:14
问题 I am new to Angular, and am trying to use it to set focus on an input with the id "input1". I am using the following code: @ViewChild('input1') inputEl: ElementRef; then later in the component: this.inputEl.nativeElement.focus(); But it isn't working. What am I doing wrong? Any help will be much appreciated. 回答1: component import { Component, ElementRef, ViewChild, AfterViewInit} from '@angular/core'; ... @ViewChild('input1') inputEl:ElementRef; ngAfterViewInit() { setTimeout(() => this

Limit input field to two decimal places - Angular 5

白昼怎懂夜的黑 提交于 2019-12-17 11:04:38
问题 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? 回答1: First create Directive for limit the two decimal places in typescript like this: import { Directive, ElementRef, HostListener } from '