2016-12-11 31 views
7

में ngSwitch का उपयोग कैसे करें पिछले दो दिनों से मैं कोणीय 2.1.0 में काम करने के लिए ngSwitch प्राप्त करने का प्रयास कर रहा हूं। लेकिन यह काम पाने के लिए असंभव लगता है।कोणीय 2

मुझे हमेशा NgSwitch के लिए कोई प्रदाता नहीं मिलता है। नीचे मेरी कोड है -

खाका -

<template [ngSwitch]="buttonSelector"> 
    <a class="btn" [ngClass]="buttonSizeClass" *ngSwitchCase="'link'" href="#"> 
    <span class="btn__text"> 
     <ng-content></ng-content> 
    </span> 
    </a> 
</template> 

घटक -

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; 
import { CommonModule } from '@angular/common'; 
import { RouterModule } from '@angular/router'; 
import { HeaderComponent } from './header/header.component'; 
import { FooterComponent } from './footer/footer.component'; 
import { InkedBtnComponent } from './inked-btn/inked-btn.component'; 

@NgModule({ 
    imports: [ 
    CommonModule, 
    RouterModule 
    ], 
    declarations: [HeaderComponent, FooterComponent, InkedBtnComponent], 
    exports: [HeaderComponent, FooterComponent, InkedBtnComponent], 
    schemas: [CUSTOM_ELEMENTS_SCHEMA] 
}) 
export class SharedModule { } 

यह साझा मॉड्यूल फिर मुख्य मॉड्यूल में आयात किया जाता है -

import { Component, OnInit, Input } from '@angular/core'; 


@Component({ 
    selector: 'app-inked-btn', 
    templateUrl: './inked-btn.component.html', 
    styleUrls: ['./inked-btn.component.css'], 
    inputs: ['buttonSize', 'buttonType', "buttonSelector"] 
}) 
export class InkedBtnComponent implements OnInit { 
    public buttonSize: string; 
    public buttonType: string; 
    public buttonSelector: string; 
    public buttonSizeClass: any; 

    constructor() { } 

    ngOnInit() { 
    this.buttonSizeClass = { 
     'btn--lg': this.buttonSize === 'large', 
     'btn--sm': this.buttonSize === 'small', 
     'btn--primary': this.buttonType === 'primary' 
    } 
    } 

} 

नीचे मेरी मॉड्यूल विन्यास है ।

मिस कहां है?

+0

आप BrowserModule आयातित है? – Milad

+0

@ xe4me ब्राउज़र मॉड्यूल मुख्य मॉड्यूल में आयात किया गया है –

उत्तर

9

https://angular.io/docs/ts/latest/api/common/index/NgSwitch-directive.html

ngSwitch केवल तरह <div>

केवल ngSwitchCase असली तत्वों पर एक <template> तत्व पर नहीं किया जा सकता, लागू किया जा सकता करने के लिए <template> से ngSwitch आयात करने की आवश्यकता तत्व

<template [ngSwitchCase]="match_expression_3"> 

वैकल्पिक रूप से के बाद से अंतिम ng-container अधिक आम वाक्य रचना के साथ <template> के बजाय इस्तेमाल किया जा सकता:

<ng-container *ngSwitchCase="match_expression_3"> 
+0

div को tamplate बदलने के बाद मुझे त्रुटि मिलती है - टेम्पलेटरफ के लिए कोई प्रदाता नहीं! –

+0

क्या आप https://plnkr.co/edit/HkKZlnBnLHikk0ciAPVH?p=preview में पुन: पेश करने का प्रयास कर सकते हैं? –

+0

मेरा बुरा। यह ठीक काम कर रहा है। मैंने [ngSwitchCase] ​​का उपयोग किया * ngSwitchCase के instaed। –

1

आप angular2/common

import {NgSwitch} from 'angular2/common' 
+0

मैंने सामान्य मॉड्यूल –

+0

आयात किया है यदि आप आयात को कम करना चाहते हैं, तो उदाहरण के लिए कुछ एनजीसी असंगतता, या क्योंकि पेड़ हिलाने से दस्तावेज़ों में इतना अच्छा काम नहीं किया जाएगा । – peterh