I have a custom component that applies bootstrap form group to my form fields. In my component I have below properties:
import { Component, Input, ContentChild } from '@angular/core';
import { NgControl } from '@angular/common';
@Component({
selector: 'form-field',
template: `
<div class="form-group" [ngClass]="{'has-error':(state && !state.valid && state.touched)}">
<label *ngIf="label" [attr.for]="state.name" class="col-sm-3 control-label">{{label}}</label>
<div class="col-sm-9">
<ng-content></ng-content>
<div *ngIf="state && !state.valid && state.errors && state.touched" class="help-block text-danger">
<span *ngIf="state.errors.required">{{label? label:'This field'}} is required</span>
<span *ngIf="state.errors.min">{{label? label:'Value'}} too small</span>
</div>
</div>
</div>
`
})
export class FormFieldComponent{
@Input()
label: string;
@ContentChild(NgControl) state;
}
And in my template I use my component like this:
<form [ngFormModel]="form" (ngSubmit)="onSubmit()" novalidate>
<form-field label="First Name">
<input ngControl="firstName" type="text">
</form-field>
</form>
I was wondering is there any way to dynamically set the placeholder text for the control through my component?
I want the label to be set as placeholder of the input field i.e.
You could bind your label to "placeholder"
This directive is applied to all input elements that have a
ngControl
or[ngControl]="..."
attribute. It sets theplaceholder
attribute on the element where it is applied.It is made globally available using
In the
FormField
component this directive is queried for and the label from the input is passed to the directive (inngOnChanges
orngAfterContentChecked
- basically the first occurence wherelabel
andstate
are available.This is just to demonstrate how its used:
Plunker example
I choose this approach because I wasn't able to query by something else (
NgControl
, ...) and get a reference to the input element.It's not necessary to provide the directive this way. It can also be provided like any other custom directive by adding it to
directives: [InputLabel]
on the@Component()
decorator where it is used.