【问题标题】:Angular 5 can't bootstrap select directive it's not workingAngular 5 无法引导选择指令它不起作用
【发布时间】:2019-01-28 22:43:21
【问题描述】:

我有一个 Angular 5 项目,我想在我的组件视图中使用 bootstrap-select 插件。

我有一个指令。

@Directive({
// tslint:disable-next-line:directive-selector
selector: '[bootstrapSelect]',
exportAs: 'bootstrap-select'
})
export class BootstrapSelectDirective implements OnInit, OnDestroy {


private changedSubscription: Subscription;
private shownSubscription: Subscription;
private hiddenSubscription: Subscription;

@Input()
required: string;

@Input()
set ngModel(values: string | string[]) {
    setTimeout(() => this.selected = values);
}


@Output()
ngModelChange = new EventEmitter();

@Output()
shown = new EventEmitter();

@Output()
hidden = new EventEmitter();


constructor(private el: ElementRef) {
    // tslint:disable-next-line:max-line-length
    this.changedSubscription = Observable.fromEvent($(this.el.nativeElement), 'changed.bs.select').subscribe((e: any) => setTimeout(() => this.ngModelChange.emit(this.selected)));
    // tslint:disable-next-line:max-line-length
    this.shownSubscription = Observable.fromEvent($(this.el.nativeElement), 'shown.bs.select').subscribe((e: any) => setTimeout(() => this.shown.emit()));
    // tslint:disable-next-line:max-line-length
    this.hiddenSubscription = Observable.fromEvent($(this.el.nativeElement), 'hidden.bs.select').subscribe((e: any) => setTimeout(() => this.hidden.emit()));
}



ngOnInit() {
    $(this.el.nativeElement).selectpicker();

    if (this.requiredAttribute) {
        $(this.el.nativeElement).selectpicker('setStyle', 'required', 'add');
    }

    setTimeout(() => {
        this.refresh();
        this.doValidation();
    });

}


ngOnDestroy() {
    if (this.changedSubscription) {
        this.changedSubscription.unsubscribe();
    }

    if (this.shownSubscription) {
        this.shownSubscription.unsubscribe();
    }

    if (this.hiddenSubscription) {
        this.hiddenSubscription.unsubscribe();
    }

    $(this.el.nativeElement).selectpicker('destroy');
}

我在一个简单的页面中使用它。

<div class="form-group">
    <label for="homePage" class="col-sm-3 control-label">{{'preferences.HomePage' | translate}} </label>
    <div class="col-sm-4">
        <select id="homePage" [(ngModel)]="configurations.homeUrl" #homePageSelector="bootstrap-select" bootstrapSelect class="selectpicker form-control">
            <option data-icon="fa fa-tachometer" data-subtext="(Default)" value="/">{{'preferences.Dashboard' | translate}}</option>
            <option data-icon="fa fa-cog" value="/settings">{{'preferences.Settings' | translate}}</option>
        </select>
    </div>
    <div class="col-sm-5">
        <p class="form-control-static text-muted small">{{'preferences.HomePageHint' | translate}}</p>
    </div>
</div>

但是当我尝试在 form 中使用指令时,我得到了那个错误

ERROR Error: Uncaught (in promise): Error: Template parse errors:There is no directive with "exportAs" set to "bootstrap-select" ("-lg-9 col-md-9"&gt; &lt;select class="form-control selectpicker" [ERROR -&gt;]#classRoomTypeIdSelector="bootstrap-select" bootstrapSelect name="classRoomTypeId" [(ngModel)]="class")

这是我尝试使用该指令的代码。

<div class="form-group">
                            <div class="col-lg-6">
                                <label class="col-lg-3 col-md-3 control-label">Tipo</label>
                                <div class="col-lg-9 col-md-9">
                                    <select class="form-control selectpicker" #classRoomTypeIdSelector="bootstrap-select" bootstrapSelect name="classRoomTypeId" [(ngModel)]="classroomModel.classRoomTypeId" formControlName="classRoomTypeId">
                                        <option *ngFor="let option of classroomTypes" [value]="option.id">{{option.name}}</option>
                                    </select>
                                    <ul class="text-danger list-unstyled" *ngIf="classroomForm.controls['classRoomTypeId'].touched && classroomForm.controls['classRoomTypeId'].invalid">
                                        <li *ngIf="classroomForm.controls['classRoomTypeId'].errors.required">
                                            El tipo del aula es requerido
                                        </li>
                                    </ul>
                                </div>
                            </div>

知道为什么我可以在一个地方使用该指令而另一个地方不行吗?

【问题讨论】:

    标签: angular angular-directive bootstrap-select


    【解决方案1】:

    您还需要在该模块的declarations 中添加该指令,以使其对该模块的组件可见

    @NgModule({ 
       ...
       declarations: [ BootstrapSelectDirective ]
       ...
    })
    export class ThatModule { }
    

    【讨论】:

    • 该指令被添加到app.module,因为在模块外使用,我尝试将它添加到两个模块中但出现错误。
    • 不能添加到两个模块中。相反,您可以创建一个共享模块,然后添加到导入并从模块导出。然后使用 SharedModule
    猜你喜欢
    • 2016-10-17
    • 1970-01-01
    • 2015-07-26
    • 2015-12-26
    • 2018-11-27
    • 2021-03-22
    • 1970-01-01
    相关资源
    最近更新 更多