Trying to move select element

I am trying to create a directive called select that replaces the select element. To be more precise, it hides the select element and shows a custom drop-down menu. Here is the jsfiddle in which I am trying to do this.

So, suppose the following select element:

<select ng-model="selectedOption">
    <option ng-repeat="option in options" ng-bind="option.value"></option>
</select>

Now this select directive should translate this code into the following template:

<section>
    <div ng-transclude></div>
    <ol> <!-- custom dropdown/select -->
        <li ng-repeat="option in options" ng-bind="option.value">
    </ol>
</section>

And the directive:

myApp.directive('select', function() {
    return {
        templateUrl: 'view/select.html',
        restrict: 'E',
        transclude: true,
        scope: {},
        link: function(scope, element, attrs) {}
    };
});

Now I see a couple of problems:

  • Transclude seems to replace the whole template (see jsfiddle )
  • there is a strange element option
  • how do i get the attached data because i need this to create a custom dropdown

Hope someone can help me here!

Thnx

UPDATE: , , , select, - - . , , select. , , $prestine, up2date

UPDATE2: , : jsfiddle. , :( !

+4
1

Update2 , , .

:
  scope: { name: '@', options: '=' },

HTML:
  <selectx name="bar" ng-model="selectedOption" options='options'>

jsfiddle.


select:
myApp.directive('select', function($compile) { return { restrict: 'E', link: function(scope, element, attrs) { var custom = angular.element('<section>\ <ol>\ <li ng-repeat="option in options" ng-bind="option.value">\ </ol>\ </section>'); custom.insertAfter(element); $compile(custom)(scope); } }; });

.

, , insertAfter , .

+1

Source: https://habr.com/ru/post/1530292/


All Articles