1

I am working on an angular directive and i am trying to prepend an element.

.directive('someDirective', function($compile){
  restrict: "E",
  scope:{
   items="@"
  }
  link: function(scope,elem,attrs){
    // selector 
    angular.element(div > ul).prepend($compile('<p> Hello World <p>') (scope))
  },
  template:`
  <div class="container"> 
    <ul class="foo">
      <li ng-repeat="item in items"> {{ item }} </li> 
    </ul>
  </div>`
});

it appends inside ul. How am i supposed to append the element after the ul tag?

<div class="container"> 
    <ul class="foo">
      <!-- prepend element goes here :( --> 
      <li ng-repeat="item in items"> {{ item }} </li> 
    </ul>
 <!-- I want the prepended element to go here -->
  </div>`

i believe there is something wrong with my selector.

1 Answer 1

1

To place an element after another element, and not inside it, you can use after(), it's part of Angulars jqLite as well

angular.element(div > ul).after($compile('<p> Hello World <p>') (scope))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.