2017-06-14 109 views
0

我有,我定义的单选按钮,一个指令,我想调用组件.. 的方法Whene我注入的构造函数,我得到这个错误的组件:ERROR Error: No provider for FoldersComponent! 错误:没有提供程序组件

directive.ts

import { FoldersComponent } from '../folders/folders.component'; 
import { Directive, ElementRef, HostListener, Input, AfterViewInit, ViewChild, EventEmitter, Output } from '@angular/core'; 

declare var jQuery: any; 

@Directive({ 
    selector: '[icheck]' 
}) 

export class IcheckDirective implements AfterViewInit { 

    @Output('icheck') 
    callComponentFunction: EventEmitter<any> = new EventEmitter<any>(); 
    constructor(private el: ElementRef, private parentCmp: FoldersComponent) { 
    jQuery(this.el.nativeElement).iCheck({ 
     checkboxClass: 'icheckbox_square-aero', 
     radioClass: 'iradio_square-aero' 
    }).on('ifChecked', function(event) { 
     if (jQuery('input').attr('type') === 'radio') { 
     this.parentCmp.selectType(); 
     } 
    }); 
    } 

    ngAfterViewInit(): void { 
    } 
} 

folder.component.ts

@Component({ 
    selector: 'app-folders', 
    templateUrl: './folders.component.html', 
    styleUrls: ['./folders.component.css'], 
}) 
export class FoldersComponent implements OnInit { 
    constructor(
    private route: ActivatedRoute, 
    private router: Router 
) { } 

    selectType() { 
    // alert(); 
    console.log("Ici"); 
    } 
} 

folder.compo nent.html

<input type="radio" icheck name="filters.type" [(ngModel)]="filters.type"     value="IN"> (IN) 

我@NgModule

@NgModule({ 
    declarations: [ 
    AppComponent, 
    LoginComponent, 
    FoldersComponent, 
    ], 
+1

它应该工作https://plnkr.co /编辑/ ecPrqOp4nDIG71vo4ksT?p =预览 – yurzui

+0

很酷... thx ..;我不明白在我的代码当我删除我的构造函数'私人parentCmp:FoldersComponent'的参数,它是工作,但是当我添加它使用组件的方法我得到错误..非常有趣.. – user1814879

+0

Thxxx这么多它是工作 – user1814879

回答

0

试试这个,你有没有导入您的组件。

import { FoldersComponent } from '../path/to/component 
constructor(private _foldersComponent : FoldersComponent){} 
@Directive({ 
selector: '[icheck]' 

}) 
... rest of file 

然后从FoldersComponent调用方法只需使用_foldersComponent。

例如,this._foldersComponent.someMethod()

+0

如果您想要现有引用组件 – yurzui

+0

,则不应在providers数组中提供组件。当然,我一定感到困惑。 – nagrom97

+0

我已经导入了我的组件,但我得到了错误.. – user1814879

0

看来你已经错过了注册FoldersComponent@NgModule({})

你能后,你必须用你的主文件NgModule

相关问题