Help us understand the problem. What is going on with this article?

Angularで検索可能なセレクトボックスを設置する

More than 1 year has passed since last update.

検索可能なSelectBoxを設置したく調べてみたところ、「Select2」というJQueryプラグインがAngularでも使用可能だったので使ってみました。

こんな感じ
スクリーンショット 2018-09-10 12.22.43.png

準備

  • JQueryとselect2をインストール
    yarn add jquery --save
    yarn add select2 --save

  • angular.jsonに追加

"scripts": [
  "node_modules/jquery/dist/jquery.js",
  "node_modules/select2/dist/js/select2.min.js"
]
  • ng2-select2をインストール
    yarn add ng2-select2 --save

  • ***.module.tsに追加

import { Select2Module } from 'ng2-select2';

@NgModule({
  imports: [
    ...,
    Select2Module
  ],
})

実装

  • ***.component.htmlに追加
<select2 [data]="exampleData"></select2>
  • ***.component.tsにSelectデータを追加
import { Select2OptionData } from 'ng2-select2';

exampleData: Array<Select2OptionData> = [
    {
      id: 'basic1',
      text: 'Basic 1'
    },
    {
      id: 'basic2',
      disabled: true,
      text: 'Basic 2'
    },
    {
      id: 'basic3',
      text: 'Basic 3'
    },
    {
      id: 'basic4',
      text: 'Basic 4'
    }
  ];
}

これで完成です。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした