Angular2 maxLength for textarea variable as variable

To set the maximum number of letters for textarea, we achieve this by writing: <textarea maxLength="50"></textarea>

but could i do something like below? Is there any way?

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

@Component({
  moduleId: module.id,
  selector: 'app-create-offer-page',
  template: `
    <textarea maxLength="textareaLength"></textarea>
  `
})
export class CreateOfferPageComponent {
  textareaLength: number = 50;
}

I ask because it does not work.

+4
source share
3 answers

You will need []for Angular to interpret it as a binding

 [maxLength]="textareaLength"

This should work for your own verification. Angular2 validators do not currently support dynamic values.

+11
source

Of course, and this is pretty simple - just use attribute binding like this:

<textarea [attr.maxLength]="textareaLength"></textarea>
+4
source

, @rinukkusu, , , :

<textarea [attr.maxLength]="textareaLength > 0 ? textareaLength : null"></textarea>

The value nullcompletely removes the attribute from the element, which was something needed.

+1
source

All Articles