2016-09-29 23 views
6

मैं एक 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

अंश,

गुण

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.

बाइंडिंग यहाँ एक प्रासंगिक स्टैक ओवरफ़्लो पद the difference between properties and attributes बारे में है।

आप नीचे दिए गए कोशिश कर सकते हैं,

@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- बाइंडिंग्स) पर बाध्यकारी विशेषता के बारे में पढ़ सकते हैं, चीयर्स !! –

संबंधित मुद्दे