我是Angular 2的新人,我有以下问题.我试图将组件属性绑定到输入的本机属性(maxlength),我无法做到这一点.
代码如下:
textBox.ts
- @Component({
- selector: 'icb-textBox',inputs: [
- 'placeholder','mxlength','enabled','mandatory','description','type'],templateUrl: 'Common/Components/TextBox/textBox.html',styleUrls: ['Common/Components/TextBox/textBox.css']
- })
- export class TextBox {
- private placeholder: string;
- private mxlength: number;
- private enabled: boolean;
- private mandatory: boolean;
- private description: string;
- private type: string;
- }
textBox.html
- <input type="text" maxlength="{{mxlength}}" [(ngModel)]="value" placeholder="{{placeholder}}" [disabled]="!enabled"/>
在’父亲’组件中:
- <icb-textBox placeholder="Name"
- mxlength="4"
- [mandatory]="false"
- [enabled]="true"
- description="Put your name">
属性’占位符’和’禁用’工作正常,但我可以使maxlength工作.
我尝试使用[maxlength]并得到此错误:无法绑定到’maxlength’,因为它不是已知的本机属性.
谢谢.
使用
- [attr.maxlength]= 'your value'
因为默认的角度看属性绑定.告诉angular显式使用我们已经使用了这种语法