Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
Angular how to wrap input value with double curly braces

What I want to achieve is that when I write inside input field "Foo" it will become {{Foo}}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

First create this directive :

@Directive({
  selector: '[format-input]',
})

export class FormatDirective implements DoCheck {

  valueIsNull:boolean = true;

  constructor(public _elementRef: ElementRef<HTMLInputElement>,
    private _renderer: Renderer2) { }

  ngDoCheck(): void {

    setTimeout(() => {
      if(this.valueIsNull){
        this.format();
      }
    }, 150)

    fromEvent(this._elementRef.nativeElement, 'blur')
      .pipe(
        debounceTime(150),
        distinctUntilChanged(),
        tap(() => {
          this.format();
        })
      )
      .subscribe();
  }

  format(){
    this._elementRef.nativeElement.value = "{{ " + this._elementRef.nativeElement.value + " }}"
    this.valueIsNull = false;
  }
}

Then Import it to your module : e.g app.module :

@NgModule({
  declarations: [
    FormatDirective
  ],
  imports: [CommonModule],
  exports: [
    FormatDirective
  ]
})
export class AppModule { }

Then you can use it anywhere you want :

<input type="text" format-input />
about 4 years ago · Juan Pablo Isaza Report

0

You should use angular templating to achieve this Official Documentation for templating and interpolation and a code sample is given below. this will help you to achieve your usecase.

https://angular.io/guide/interpolation https://angular.io/api/forms/NgModel

import {Component} from '@angular/core';
import {NgForm} from '@angular/forms';

@Component({
  selector: 'example-app',
  template: `
    <form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
      <input name="first" ngModel required #first="ngModel">
      <input name="last" ngModel>
      <button>Submit</button>
    </form>

    <p>First name value: {{ first.value }}</p>
    <p>First name valid: {{ first.valid }}</p>
    <p>Form value: {{ f.value | json }}</p>
    <p>Form valid: {{ f.valid }}</p>
  `,
})
export class SimpleFormComp {
  onSubmit(f: NgForm) {
    console.log(f.value);  // { first: '', last: '' }
    console.log(f.valid);  // false
  }
}

Thanks

Rigin Oommen

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!