Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have implemented a custom select2 dropdown directive. And register the change event handler to element which directive link to. The change event of the input element will be triggered when I selected a item from dropdown list. Everything works as expected. You could try my implementation:</p> <p>Directive</p> <pre><code>.directive('ajaxSelect2',['$timeout', function($timeout){ return { restrict: 'A', link: function(scope, element, attrs){ element.select2({ width: '200px', createSearchChoice:function(term, data) { if ($(data).filter(function() { return this.text.localeCompare(term)===0; }).length===0) {return {id:term, text:term};} }, multiple: true, data: [{id: 0, text: 'story'},{id: 1, text: 'bug'},{id: "ccc", text: 'task'}] }); element.bind("change",function(e){ scope.$apply(function(){ scope[attrs.ngModel] = e.val.toString(); }); }); } }; }]); </code></pre> <p>HTML</p> <pre><code>&lt;body ng-app="selectApp"&gt; &lt;div ng-controller="selectCtrl"&gt; &lt;span&gt;Selcted value: {{input}}&lt;/span&gt; &lt;div&gt; &lt;input id="test2" ng-model="input" ajax-select2/&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <p>Here is the <a href="http://jsbin.com/eHebOti/1/edit" rel="nofollow">JSBin DEMO</a></p> <p><strong>[EDIT] For Property access problem</strong></p> <p>The ng-model of custom select2 element is "filter.foo". You want to notify the angular world that the value of ng-model have been updated by select2 dropdown filter. But you can't access the second level property like this:</p> <pre><code>scope[attrs.ngModel] = e.val; // equal to scope['filter.foo'] which is the first level property 'filter.foo' </code></pre> <p>You should access the property in this way:</p> <pre><code>var props = attrs.ngModel.split("."); if(props.length&gt;1){ scope[props[0]][props[1]] = e.val; //[] equals to dot '.' }else{ scope[attrs.ngModel] = e.val; } </code></pre> <p><strong>Here is a <a href="http://jsfiddle.net/h4763/" rel="nofollow">jsFiddle demo</a></strong></p> <p>Hope this is helpful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload