はひとつのアイデアが最も推奨角度の方法ではありませんDOM操作を使用することですが、私はあなたの非同期呼び出しをシミュレートするためにworking on this plunker.とsecond one with custom directive and randomized data to simulate your compiled avatar.
それを得た、私はngResourceを使用しています。私のレンダリング関数は、文字列"<div class='compiledavatar'>Temporary Avatar</div>"
と特別なクラスマークcompiledavatar
を返します。 2〜2回は、要素を選択すると一時アバターが表示されます。 ngResourceが終了したら、クラスcompiledavatar
の要素を探し、htmlをダウンロードしたものに置き換えます。ここでは、完全なコードは次のとおりです。
var app = angular.module('plunker', ['selectize', 'ngResource']);
app.controller('MainCtrl', function($scope, $resource, $document) {
var vm = this;
vm.name = 'World';
vm.$resource = $resource;
vm.myModel = 1;
vm.$document = $document;
vm.myOptions = [{
id: 1,
title: 'Spectrometer'
}, {
id: 2,
title: 'Star Chart'
}, {
id: 3,
title: 'Laser Pointer'
}];
vm.myConfig = {
create: true,
valueField: 'id',
labelField: 'title',
delimiter: '|',
placeholder: 'Pick something',
onInitialize: function(selectize) {
// receives the selectize object as an argument
},
render: {
item: function(item, escape) {
var label = item.title;
var caption = item.id;
var Stub = vm.$resource('mydata', {});
// This simulates your asynchronous call
Stub.get().$promise.then(function(s) {
var result = document.getElementsByClassName("compiledavatar")
angular.element(result).html(s.compiledAvatar);
// Once the work is done, remove the class so next time this element wont be changed
// Remove class
var elems = document.querySelectorAll(".compiledavatar");
[].forEach.call(elems, function(el) {
el.className = el.className.replace(/compiledavatar/, "");
});
});
return "<div class='compiledavatar'>Temporary Avatar</div>"
}
},
// maxItems: 1
};
});
私はちょうどplunker mydata
でファイルを作成したJSON APIをシミュレートするには:
{
"compiledAvatar": "<div><span style='display: block; color: black; font-size: 14px;'>an avatar</span></div>"
}
もちろん、あなたのコンパイルされた関数はあなたに別の何かすべての呼び出しを返す必要があります。私は原理を実証するために私に同じことを与えます。
:
は、データがカスタムディレクティブmy-customer
が含まれます:あなたの動的なコードがAgularディレクティブがある場合は、よりよい解決策を見ることができるように、また
は、ここでカスタムディレクティブでsecond plunkerとランダム化されたデータをあります
[{
"compiledAvatar": "<div><span style='display: block; color: black; font-size: 14px;'>an avatar #1 <my-customer></my-customer></span></div>"
},
{
"compiledAvatar": "<div><span style='display: block; color: black; font-size: 14px;'>an avatar #2 <my-customer></my-customer></span></div>"
},
(...)
ディレクティブは次のように定義されます
app.directive('myCustomer', function() {
return {
template: '<div>and a custom directive</div>'
};
});
とアプリの主な違いは、あなたヘクタールでありますHTMLを置き換えるときに$ compileを追加すると、テキストはAn avatar #(number) and a custom directive
と表示されます。私はjsonの値の配列を取得し、単純なランダムを使用して値を選択します。 HTMLが置き換えられたら、クラスを削除するので、次に最後に追加された要素だけが変更されます。
Stub.query().$promise.then(function(s) {
var index = Math.floor(Math.random() * 10);
var result = document.getElementsByClassName("compiledavatar")
angular.element(result).html($compile(s[index].compiledAvatar)($scope));
// Remove class
var elems = document.querySelectorAll(".compiledavatar");
[].forEach.call(elems, function(el) {
el.className = el.className.replace(/compiledavatar/, "");
});
});
また、私はselectize libraryを見て、htmlと同様に約束を返すことはできません。レンダリングによって返された値に置き換えます。これが、私が後で検索して更新するクラスを持つ一時的な文字列のルートに行った理由です。 それが役立つかどうか教えてください。
angle-selectizeのgithubの問題として追加されました:https://github.com/machineboy2045/angular-selectize/issues/142 – atfornes
これは、推奨する方法ではないかもしれませんが、あなたが望む行動? – Pytth
@Pythth、約束がどのように役立つのかわかりません、 'render.item'関数は約束ではなくhtml' string'を返す必要があることに注意してください。この問題の約束をどのように使用するかに関するアイデアをお寄せください。ありがとう! – atfornes