将组件属性绑定到输入的本机属性

Her*_*tos 7 angular

我是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;
}
Run Code Online (Sandbox Code Playgroud)

textbox.html

 <input type="text" maxlength="{{mxlength}}" [(ngModel)]="value" placeholder="{{placeholder}}" [disabled]="!enabled"/>
Run Code Online (Sandbox Code Playgroud)

在'父亲'组件中:

<icb-textbox placeholder="Name" 
                 mxlength="4" 
                 [mandatory]="false" 
                 [enabled]="true" 
                 description="Put your name">
Run Code Online (Sandbox Code Playgroud)

属性'占位符'和'禁用'工作正常,但我可以使maxlength工作.我尝试使用[maxlength]并得到此错误:无法绑定到'maxlength',因为它不是已知的本机属性.

谢谢.

Par*_*ain 13

使用

[attr.maxlength]= 'your value'
Run Code Online (Sandbox Code Playgroud)

因为默认角度看属性绑定.告诉angular使用显式我们已经使用了这种语法


Gün*_*uer 11

请改用

[attr.maxlength]="someValue"
Run Code Online (Sandbox Code Playgroud)

要么

attr.maxlength="{{someValue}}"
Run Code Online (Sandbox Code Playgroud)

显式绑定到属性而不是属性.