0

I am developing an Angular2 app and to make mobile friendly need to disable autocomplete, autocorrect, autocapitalize, and spellcheck for all html inputs. I do not want to have to manually add this for every single input and would like a dynamic site way method to implement.

For example in jQuery we could just do $('input').attr('autocomplete','off')

How can I accomplish the same in Angular2? To grab any inputs sitewide and modify their attributes?

1 Answer 1

1

You could create an directive for this, Docs

It would look something like this: Untested code that needs to be edited for your project

import {Directive, Input, Renderer} from '@angular/core';

@Directive({
  selector: 'input',
  host: {
    '(change)': 'setAtrributes()',
  }
})
export class InputDirective {
  @Input() mobile: boolean;

  constructor(private elementRef: ElementRef, private renderer: Renderer) {}

  setAtrributes() {
    this.renderer.setElementAttribute(this.elementRef.nativeElement, 'attributename', this.mobile ? 'off' : 'on');
    // Other attributes
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

perfect thanks, I removed the onchange and just threw them into the constructor though since I want it to always happen... didn't realize you could just tack on additional bits to existing directives via selector: 'input' like that much appreciated :)

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.