angular2-forms

Disabled input validation in dynamic form

本秂侑毒 提交于 2019-12-22 08:47:01
问题 I have a dynamic form (made an example using angular.io dynamic form live example plunkr) and I want to disable an input of this form, to display it as a readonly information. So I decided to add disabled attribute to the question model: export class QuestionBase<T>{ value: T; key: string; label: string; required: boolean; order: number; controlType: string; disabled?:boolean; constructor(options: { value?: T, key?: string, label?: string, required?: boolean, order?: number, controlType?:

Bind Select List with FormBuilder Angular 2

余生长醉 提交于 2019-12-22 08:41:10
问题 In Angular 2, creating a simple app but when formBuilder is attached with DOM control in case of select list , First option is coming blank - even if I provided some initial value in formBuilder that value is not coming to DOM element Before FomBuilder Select List Genral LDAP After FomBuilder Select List No option appearing initially Genral LDAP File 1 - form.component.ts import { Component } from '@angular/core'; import { FormBuilder, Validators, REACTIVE_FORM_DIRECTIVES, FormGroup,

What's the difference between ref- prefix and # in Template reference variable (Angular 2)

亡梦爱人 提交于 2019-12-22 08:40:12
问题 I want to understand the difference between template reference variable notations as mentioned below in the input text boxes. <input type="text" name='name' #name [(ngModel)]='model'> <input type="text" name='name' ref-name [(ngModel)]='model'> What's the difference between using #name and ref-name . Does the scope of the variable changes on using ref-name ? Can anybody please suggest the best practice and reason ? 回答1: They are two different syntaxes for literally exactly the same thing. You

Applying angular2 form directives to custom input form elements

寵の児 提交于 2019-12-22 08:16:56
问题 I want to create a custom InputCustom component and use it to create model-driven forms. My custom component just wraps an input field and uses Bootstrap material design for look'n'feel. @Component({ selector:'inputCustom', template:` <div class="form-group label-floating is-empty"> <label class="control-label" for="input">Type here</label> <input class="form-control" id="input" type="text"> <p class="help-block">Some help text</p> <span class="material-input"></span> </div> `}) class

How to programmatically change pristine property in angular 2 form without ng-model

大兔子大兔子 提交于 2019-12-22 07:56:11
问题 I'm new in Angular 2 framework. I appreciate any help. I have usual component in angular 2: import {FORM_DIRECTIVES, FormBuilder, Validators} from 'angular2/common'; export class TestComponent { public values = ['value1', 'value2', 'value3', 'value4']; } Then I'm injecting FormBuilder into the constructor function: @Inject(FormBuilder) public fb HTML contain next markup: <input [(ngModel)]="formData.title" type="text" class="form-control" ngControl="title"> Title and description works great.

How to implement automatic form validation in form with nested component?

邮差的信 提交于 2019-12-22 00:27:04
问题 Consider the following simple example in a component of an Angular 4 application. It shows a simple HTML form with two input fields. One input field is directly implemented, the second is within a child component: <form #personForm="ngForm"> <input type="text" required name="firstname [(ngModel)]="firstname"/> <app-smart-input required [(model)]="lastname"></app-smart-input> <button [disabled]="personForm.valid === false">Send</button> </form> The child component is defined as follows: import

Angular2 - Enter Key executes first (click) function present on the form

不羁岁月 提交于 2019-12-21 17:14:40
问题 I don't know why each time I press ENTER key in an input element the application execute the first function it finds in the html . How Can I prevent it ? I tried to make some test with (keyup.enter) event but it first execute the first (click) function, then the the (keyup.enter) function... This is the code: html form <form role="form" [formGroup]="FormOffertDetail" (keyup.enter)="checkKey()"> <p-dialog modal="modal" [(visible)]="displayModal_Duplicate" width="300" responsive="true"> <p

select list set selected item angular 2 ngModel

假装没事ソ 提交于 2019-12-21 16:53:27
问题 This is my current code: <select name="role" [(ngModel)]="user.role"> <option *ngFor="let role of roles" [ngValue]="role" [attr.selected]="role == user.role ? 'true' : 'false'">{{role.name}}</option> </select> I'm loading all the roles in an array, and the user class has a Role attribute (which is not loaded like user.role = roles[0] but just through the backend data). The problem is that the selected attribute is not working, and my select is not going to the correct role. What am I doing

Angular 2 / 4 : How to POST HTML form (Not ajax) thru component on callback of 1st ajax submit?

早过忘川 提交于 2019-12-21 11:46:05
问题 I want to submit form to external site by POSTing the input fields in old school way(non Ajax) , it submits too but Angular giving me error in console before jumping to external page. I used following code in HTML(template) <form (submit)="onSubmit($event)" method="POST" [formGroup]="form" *ngIf='form' action="https://www.sandbox.paypal.com/cgi-bin/webscr" > In component onSubmit(obj: any) { if (!this.form.valid) { this.helper.makeFieldsDirtyAndTouched(this.form); } else { this.loader = true;

angular2 validate form on button click

烈酒焚心 提交于 2019-12-21 07:38:48
问题 If I submit a form using button type="submit" the form validation messages appear and everything is fine. However if I have a button (or link) with (click)="myhandler()" then the validations do not appear. How can I either: tag the element as requiring validators to run, or programatically run and show validation messages. Note: These are simple validations like required on input fields. Sample Code: <form (ngSubmit)="save()"> <input required type='text' [(ngModel)]="name"> <!-- Shows