在 typeScript 中注入 select2

inject select2 in typeScript

我在 TypeScript/Angular 项目中注入 select2 库时遇到问题。我不知道还能做什么才能使其正常工作,我搜索了一些但没有成功的工作示例。

模块是这样注入的:

public init() {
        var myApp = angular.module('app', [
            'ui.router',
            'ngCookies',
            'ui.bootstrap',
            'select2'
        ]); 
...

我收到一个错误:

Module 'select2' is not available! You either misspelled the module name or forgot to load it.

我做错了什么?任何帮助表示赞赏。

您正在尝试将 Select2 像 Angular 模块一样注入到您的应用程序中。这是抛出错误,因为 Select2 不是 angular 模块,它是 jQuery 组件。

Angular UI 提供 ui-select 这是 Select2 的 Angular 本机版本。

所以我通过在我的控制器中直接以 jQuery 样式初始化 select2 来解决这个问题...

$('select').select2();

这可能不是很好的解决方案,但它确实有效。如果有人有更好的解决方案,请随时 post 它