textarea的Angular2 maxLength作为变量
Angular2 maxLength for textarea as variable
要设置文本区域的最大字母数,我们可以通过以下方式实现:
<textarea maxLength="50"></textarea>
但是,我可以做下面这样的事情吗?有什么办法吗?
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;
}
我问是因为那行不通。
您需要 []
才能 Angular 将其解释为绑定
[maxLength]="textareaLength"
这应该适用于本机验证。 Angular2 个验证器目前不支持动态值。
当然,这很简单 - 只需像这样使用属性绑定:
<textarea [attr.maxLength]="textareaLength"></textarea>
稍微扩展 提供的答案,您甚至可以通过添加三元运算符使属性成为可选的,如下所示:
<textarea [attr.maxLength]="textareaLength > 0 ? textareaLength : null"></textarea>
null
值从元素中完全删除属性,这是我需要的。
要设置文本区域的最大字母数,我们可以通过以下方式实现:
<textarea maxLength="50"></textarea>
但是,我可以做下面这样的事情吗?有什么办法吗?
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;
}
我问是因为那行不通。
您需要 []
才能 Angular 将其解释为绑定
[maxLength]="textareaLength"
这应该适用于本机验证。 Angular2 个验证器目前不支持动态值。
当然,这很简单 - 只需像这样使用属性绑定:
<textarea [attr.maxLength]="textareaLength"></textarea>
稍微扩展
<textarea [attr.maxLength]="textareaLength > 0 ? textareaLength : null"></textarea>
null
值从元素中完全删除属性,这是我需要的。