我試圖創建詹姆斯·史密斯的tokenInput jQuery插件的angular.js指令:http://loopj.com/jquery-tokeninputtokenInput作爲angular.js指令
這是我到目前爲止有:
antdna = angular.module('Communication', []);
antdna.factory('autoCompleteService', [function() {
return {
getSource: function() {
return [{"id":1, "name":"John Doe"}, {"id":2, "name":"Jane Smith"}];
}
}
}]);
antdna.directive('autoComplete', function(autoCompleteService) {
return {
restrict: 'A',
link: function(scope, elem) {
elem.tokenInput(autoCompleteService.getSource(), {
crossDomain:false,
theme: "facebook",
hintText: "Enter User Name",
preventDuplicates: true
});
}
};
});
用下面的標記:
<input type="text" name="recipient" ng-model="conversation.recipients" class="messageComposeTextField" auto-complete placeholder="To :" required />
TokenInput完美地工作,但我的問題是,我無法綁定到模型。
{{conversation.recipients}}
是空的。
tokenInput插件使用列表元素(ul和li)生成它自己的標記。所以在檢查元素我得到:
<ul class="token-input-list-facebook">
<li class="token-input-token-facebook"><p>John Doe</p><span class="token-input-delete-token-facebook">×</span></li><li class="token-input-input-token-facebook"><input type="text" autocomplete="off" autocapitalize="off" id="token-input-" style="outline: none; width: 30px;"><tester style="position: absolute; top: -9999px; left: -9999px; width: auto; font-size: 12px; font-family: Ubuntu, 'Ubuntu Beta', UbuntuBeta, Ubuntu, 'Bitstream Vera Sans', 'DejaVu Sans', Tahoma, sans-serif; font-weight: 400; letter-spacing: 0px; white-space: nowrap;"></tester> </li>
</ul>
<input type="text" name="recipient" ng-model="conversation.recipients" class="messageComposeTextField ng-pristine ng-invalid ng-invalid-required" auto-complete="" placeholder="To :" required="" style="display: none;">
注意,產生tokenInput標記不是指令的一部分。所以這裏真正的問題是如何封裝一個jquery插件,在angularjs指令中生成它自己的標記?
謝謝你的建議,我似乎無法讓你的小提琴上班 –
道歉,這是一個與MIME類型的Chrome的東西。在FF工作正常 –
Select2工作,只需要添加ajax選項,以將用戶數據從我的服務器。感謝您的幫助 –