【发布时间】:2017-06-14 10:05:25
【问题描述】:
我有一个指令,我在其中定义了一个单选按钮,我想调用一个组件的方法..
当我在构造函数中注入组件时,出现此错误: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 {
}
}
文件夹.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");
}
}
文件夹.component.html
<input type="radio" icheck name="filters.type" [(ngModel)]="filters.type" value="IN"> (IN)
我的@NgModule
@NgModule({
declarations: [
AppComponent,
LoginComponent,
FoldersComponent,
],
【问题讨论】:
-
太酷了...谢谢 ..;当我删除构造函数
private parentCmp: FoldersComponent的参数时,我不明白我的代码,它是有效的,但是当我添加它以使用组件的方法时,我得到了错误.. 非常有趣.. -
Thxxx 太辛苦了
-
@yurzui 你能解释一下你是怎么解决的吗?
标签: angular angular2-directives