2016-05-17 14 views
14

मैं कोणीय 2 टेम्पलेट्स में enum का उपयोग करने की कोशिश कर रहा हूं। नीचे मेरी कोड हैकोणीय 2 टेम्पलेट्स में enum का उपयोग कैसे करें

@Component({ 
    selector: 'test', 
    template: ` 
<ul class="nav navbar-nav"> 
    <li class="{{activeSection == SectionType.Primary ? 'active': ''}}"><a href="javscript:void(0);" (click)="setActiveSection(SectionType.Primary)">Primary Details</a></li> 
    <li class="{{activeSection == SectionType.Aditional ? 'active': ''}}"><a href="javscript:void(0);" (click)="setActiveSection(SectionType.Aditional)">Additional Details</a></li> 
    <li class="{{activeSection == SectionType.Payment ? 'active': ''}}"><a href="javscript:void(0);" (click)="setActiveSection(SectionType.Payment)">Payment Settings </a></li>   
    </ul>` 
    }) 
    export class TestComponent{ 
    activeSection: SectionType = SectionType.Primary; 
    setActiveSection(section: SectionType) { 
     this.activeSection = section; 
    } 
} 

यहाँ मेरी enum है:

enum SectionType { 
    Primary, 
    Aditional, 
    Payment 
} 

यह फेंकने है अपवाद: लेखन त्रुटि: अपरिभाषित

उत्तर

9

SectionType की संपत्ति 'प्राथमिक' पढ़ा नहीं जा सकता सीधे नहीं किया जा सकता टेम्पलेट के भीतर। या तो आप इसे अपने घटक के एक प्रॉपर्टी पर सेट, या तो आप की जाँच करने के तरीकों को निर्दिष्ट जोड़ें:

@Component({ 
    selector: 'test', 
    template: ` 
     <ul class="nav navbar-nav"> 
     <li class="{{isPrimarySection(activeSection) ? 'active': ''}}"><a href="javscript:void(0);" (click)="setActiveSection(SectionType.Primary)">Primary Details</a></li> 
     (...) 
     </ul> 
    ` 
    }) 
    export class TestComponent{ 
    activeSection: SectionType = SectionType.Primary; 
    setActiveSection(section: SectionType) { 
     this.activeSection = section; 
    } 
    isPrimarySection(activeSection) { 
     return activeSection == SectionType.Primary 
    } 
} 

या

@Component({ 
    selector: 'test', 
    template: ` 
     <ul class="nav navbar-nav"> 
     <li class="{{activeSection == SectionType.Primary ? 'active': ''}}"><a href="javscript:void(0);" (click)="setActiveSection(SectionType.Primary)">Primary Details</a></li> 
     (...) 
    </ul>` 
    }) 
    export class TestComponent{ 
    activeSection: SectionType = SectionType.Primary; 
    setActiveSection(section: SectionType) { 
     this.activeSection = section; 
    } 
    SectionType:SectionType = SectionType; 
    } 
+1

पहला समाधान काम कर सकता है लेकिन '(क्लिक) = "setActiveSection (sectionType.Primary)" ' और दूसरा समाधान ' सेक्शनटाइप: सेक्शनटाइप = सेक्शनटाइप; 'त्रुटि दे रहा है टाइप करें' टाइपऑफ टाइप टाइप 'टाइप करें' सेक्शन टाइप 'टाइप करने के लिए असाइन नहीं किया जा सकता है –

+3

इसके बजाए इसे आज़माएं: 'सेक्शनटाइप = सेक्शन टाइप;' –

+2

मैं 'सेक्शनटाइप: किसी भी = सेक्शन टाइप टाइप' के साथ काम करता हूं। इस plunkr देखें: http://plnkr.co/edit/Mos2zocjWxiYx5rnY4PI?p=preview। –

3

एक टेम्पलेट में एक Enum उपयोग करने के लिए आसान तरीका

@Component(...) 
export class MyComp { 
    public MyEnum: any = MyEnum; 
} 
है

फिर टेम्पलेट में:

<select> 
    <option value="MyEnum.ValueA">Value A</option> 
</select> 
संबंधित मुद्दे