0

this uppercase directive not work on angular.... if i print a console.log() to see value on format method i can see the value of i input, but the text not update to uppercase on inputText;

my declaration on html

<input type="text" uppercase >

import {Directive, Input, Output, EventEmitter, OnInit} from 'angular2/core';

@Directive({
    selector: '[uppercase]',
    host: {
        '[value]': 'uppercase',
        '(input)': 'format($event.target.value)'
    }
})
export class Uppercase implements OnInit {

    @Input() uppercase: string;
    @Output() uppercaseChange: EventEmitter<string> = new EventEmitter<string>();

    constructor() {
    }

    ngOnInit() {
        this.uppercase = this.uppercase || '';
        this.format(this.uppercase);
    }

    format(value) {
        value = value.toUpperCase();
        this.uppercaseChange.next(value);
    }
}

how i can do to text uppercase?

2
  • What do you want to archieve exactly. You may need to use your directive in a template or not ? Can you show your template code ? Commented Sep 23, 2018 at 22:15
  • hi. I updated the code, I try do it in only one input text <input type="text" appUppercaseText > Commented Sep 24, 2018 at 1:16

2 Answers 2

2

You may need to update your template like this <input type="text" uppercase > in other to apply all changes from your Directive.

But it's important to correct your logic because uppercase directive will be applied to a nativeElement like input.

So you may need to have to implement the ControlValueAccessor interface and ajust your directive.

Here a example how you can do this (I have adapted your code): Uppercase directive on stackblitz

I added some comment to the code.

Let me know if it's that what you want to archieve.


Edit (look comments below)

if you look for a easier way to create your directive and keep the two things you need Input text should be Uppercase and placeholder should be lowercase, you can simply do something like following

uppercase.directive.ts

import { Directive,ElementRef, HostListener } from '@angular/core';

@Directive({
 selector: '[uppercase]'
})
export class UppercaseDirective {
  constructor(public ref: ElementRef) { }

  @HostListener('input', ['$event']) onInput(event) {
     this.ref.nativeElement.value = event.target.value.toUpperCase();
  }
}

your.template.html

<input placeholder="placeholder lowercase" uppercase type='text'>

I made here another demo on stackblitz

Sign up to request clarification or add additional context in comments.

2 Comments

its work, thank you very much, i will study more about ControlValueAccessor
the placeholder are uppercase to, but i want placeholder lowercase and only value uppercase, you know how solve it?
0
import { Directive, HostListener } from '@angular/core';
import { NgControl } from '@angular/forms';

@Directive({
  selector: '[uppercase]'
})
export class UppercaseDirective {

  constructor(private readonly control: NgControl) { }

  @HostListener('input', ['$event.target'])
  protected onInput(input: HTMLInputElement): void {
    const caretPos = input.selectionStart;
    this.control.control.setValue(input.value.toUpperCase());
    input.setSelectionRange(caretPos, caretPos);
  }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.