2016-09-29 15 views
6

IがMAXLENGTHが設定された入力を含む以下の角度2成分をレンダリングすることができるのMAXLENGTHに結合します。私は結合を経由してMAXLENGTHを設定しようとする場合は、次のように:角度2入力又はTexAreaは

@Component({ 
    selector: 'app', 
    template: '<input [maxlength]="maxLength">', 
}) 
export class App { 
    maxLength = 10; 
} 

またはこのような:

template: '<input maxlength="{{maxLength}}">', 

私は次のエラーを取得する:

"Template parse errors: Can't bind to 'maxlength' since it isn't a known property of 'input'." 

なぜ? maxlengthは入力コントロールの完全に有効なプロパティです。

ここにはlive example(オープンコンソールでエラーが表示されます)です。 Angular documentationから

答えて

19

抜粋、ここでは、関連するスタックオーバーフローのポストについてthe difference between properties and attributes

We become painfully aware of this fact when we try to write something like this:

<tr><td colspan="{{1 + 1}}">Three-Four</td></tr> We get this 

error:

Template parse errors: Can't bind to 'colspan' since it 
isn't a known native property 

As the message says, the element does not have a colspan property. It has the "colspan" attribute, but interpolation and property binding can set only properties, not attributes.

We need attribute bindings to create and bind to such attributes.

Attribute binding syntax resembles property binding. Instead of an element property between brackets, we start with the prefix attr, followed by a dot (.) and the name of the attribute. We then set the attribute value, using an expression that resolves to a string.

バインディング

属性。

あなたは、ここで

@Component({ 
    selector: 'app', 
    template: '<input [attr.maxlength]="maxLength" >', 
}) 
export class App { 
    maxLength = '10'; 
} 


@NgModule({ 
    imports: [ BrowserModule ], 
    declarations: [ App ], 
    bootstrap: [ App ] 
}) 
export class AppModule {} 

が更新され、それがPlunker!!

は、この情報がお役に立てば幸い働く下記てみてください!

+0

ありがとうございます。それはどういう意味ですか、それはどうやって起こったのですか、それに関する文書がありますか?私はこれを私の同僚に説明する方法を知る必要があります。 – Mud

+0

あなたは[角度の書類](https://angular.io/docs/ts/latest/guide/template-syntax.html#!#other-bindings)で乾杯しています。 –

関連する問題