在 Angular 4 中为 SelectBoxIt 插件创建指令。导入 jQuery、jQuery UI 和 SelectBoxIt。指令在模块中导入并应用于选择框。它抛出以下错误:
ERROR TypeError: $(...).selectBoxIt is not a function
at MySelectBoxItDirective.ngAfterViewInit (my-select-box-it.directive.ts:14)
at callProviderLifecycles (core.js:12427)
at callElementProvidersLifecycles (core.js:12398)
at callLifecycleHooksChildrenFirst (core.js:12382)
at checkAndUpdateView (core.js:13515)
at callViewAction (core.js:13857)
at execComponentViewsAction (core.js:13789)
at checkAndUpdateView (core.js:13513)
at callViewAction (core.js:13857)
at execEmbeddedViewsAction (core.js:13815)
.angular-cli.json
"apps": [
{
...
"scripts": [
"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/jquery-ui/ui/core.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js",
"../node_modules/selectboxit/src/javascripts/jquery.selectBoxIt.min.js"
],
}
]
MySelectBoxIt 指令代码:
import {AfterViewInit, Directive, ElementRef} from '@angular/core';
@Directive({
selector: '[appMySelectBoxIt]'
})
export class MySelectBoxItDirective implements AfterViewInit {
constructor(private el: ElementRef) { }
ngAfterViewInit() {
$(this.el.nativeElement).selectBoxIt({
showFirstOption: true
}).on('open', function() {
});
}
}
如何解决这个错误?