Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>ng-transclude</code> transcludes the content of an element on which the directive was placed. I would have assigned the attribute to its parent div and transcluded the entire select box in the template:</p> <p><strong>First Approach:</strong></p> <p><a href="http://plnkr.co/edit/fEaJXh?p=preview" rel="nofollow">http://plnkr.co/edit/fEaJXh?p=preview</a></p> <pre><code> &lt;div class="form-control my-select"&gt; &lt;select class="irrelevant-class" name="reason" id="reason" data-anything="banana"&gt; &lt;option value=""&gt;Reason for Contact...&lt;/option&gt; &lt;option&gt;Banana&lt;/option&gt; &lt;option&gt;Pizza&lt;/option&gt; &lt;option&gt;The good stuff&lt;/option&gt; &lt;option&gt;This is an example of a really, really, really, really, really, really long option item&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; </code></pre> <p>And remove replace option from the definition:</p> <pre><code>app.directive('mySelect', [function () { return { template: '&lt;div class="faux-select" ng-class="{ placeholder: default == viewVal, focus: obj.focus }"&gt;' + '&lt;span class="faux-value"&gt;{{viewVal}}&lt;/span&gt;' + '&lt;span class="icon-arrow-down entypo-down-open-mini"&gt;&lt;/span&gt;' + '&lt;div ng-transclude&gt;&lt;/div&gt;' + '&lt;/div&gt;', transclude: true, //replace: true, scope: true, restrict: 'C', link: function (scope, elem, attrs) { var $select = elem.find('select'); scope.default = scope.viewVal = elem.find('option')[0].innerHTML; scope.$watch('val', function(val) { if(val === '') scope.viewVal = scope.default; else scope.viewVal = val; }); if(!scope.val) scope.val = $select.find('option[selected]').val() || ''; } }; }]); </code></pre> <p><strong>Second Approach:</strong> In your demo, just include following line at the end of the link method:</p> <pre><code>$select.attr({'id': elem.attr('id'), 'name': elem.attr('name')}); </code></pre>
 

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