私はtypeahead.jsをAngular 2で使用しようとしています(Bloodhound機能を利用するには最高の方法です)。しかし、jsファイルをプロジェクトにインポートするのは難しいです。typeahead.jsをAngular 2で使用する
私はindex.htmlの中に次き:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
と先行入力ファイル:
<script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/corejs-typeahead/1.1.1/bloodhound.min.js"></script>
私もjQueryのsystemjs.configに次のようにインポートした
:
..
map: {
'jquery': 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js',
}..
そして、私はngAfterViewInitメソッドでjQueryを持っています:
ngAfterViewInit() {
console.log("jQuery is ready");
// constructs the suggestion engine
var states = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
// `states` is an array of state names defined in "The Basics"
local: states
});
$('#bloodhound .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
source: states
});
}
jQueryが機能します(コンソールログエントリによって確認されます)。しかし、私は次のエラーを取得する:
jQuery.Deferred exception: $(...).typeahead is not a function TypeError: $(...).typeahead is not a function
と
ERROR TypeError: $(...).typeahead is not a function
みては? (ドキュメントが準備された後にトリガされるので、document.readyイベントに追加するのは意味がありません) –
document.readyイベントを削除しましたが、私はまだ同じエラーが発生しています:ERROR Error:Uncaught ):TypeError:$(...)。typeaheadは関数ではありません – user3565164
このplunkはエラーを受けません。また、ngAfterViewInitにコードがあります:https://plnkr.co/edit/Xvm0zwut4on8n6LNJVHL?p=preview index.htmlの中に - 角度スクリプトの前にtypeaheadスクリプトタグを追加しましたか? –