0
我想構建一個指令,它將下拉(選擇)鏈接到外部jquery插件。AngularJS:將使用ng-repeat的指令與外部插件集成的問題
這是指導我templateUrl鑑於代碼:
<select multiple="multiple">
<option data-ng-repeat="(key,label in opts)"
value="{{key}}">{{label}}</option>
</select>
這是我指導的鏈接功能:
link: function(scope, element, attrs)
{
console.log(scope.opts);
console.log($(element).html());
$(element).chosen(); //call external plugin
}
這裏的問題是,由於某種原因,當link
函數被調用,選擇尚未使用ng-repeat填充選項。所以當我調用jquery插件時,它會顯示一個空白的下拉列表,儘管後面的select會填充選項。
的console.log(scope.opts);
輸出正確顯示了期權的對象,而console.log($(element).html());
只能說明:
<!-- ngRepeat: (key,label) in opts -->
有當NG重複已填充選擇要通知的方式,所以我只能然後調用外部插件?
角沒有任何機制來可靠時,DOM已使檢測只需使用
ngRepeat
和ngSelected
。你最好的選擇是使用'$ timeout'。說,你爲什麼不看'choosen'指令,我在這裏找到了一個https://github.com/localytics/angular-chosen。 – Chandermani@Chandermani謝謝,但我想寫我自己的。如果我從'$ scope。$ watch'內調用'$(element).chosen()',任何一種情況下,'link.'的scope.opts變量''scope。$ watch'似乎都可以工作。 ,它似乎工作正常 –