0

I am using same divs in my view but inside of divs are changing.So I want to make it in a directive. What is the way for this? Example;

<div id="firstDiv">
  <div id="secondDiv">
     <div id="thirdDiv">
      <label....>
     </div>
  </div>
</div>

<div id="firstDiv">
  <div id="secondDiv">
     <div id="thirdDiv">
      <select....>
     </div>
  </div>
</div>

<div id="firstDiv">
  <div id="secondDiv">
     <div id="thirdDiv">
      <textArea....>
     </div>
  </div>
</div>

So i want to make this part as a directive ;

   <div id="firstDiv">
      <div id="secondDiv">
         <div id="thirdDiv">
         </div>
      </div>
   </div>

Can you help ?

3
  • Directives are used only when you have a similar structure with similar models but each might change independently, like instance of some class. The model of each will be same in directive but might or might not change the values of others. What you are asking seems vague in this scenario. Commented Apr 12, 2016 at 9:02
  • Are you looking for ng-transclude? Commented Apr 12, 2016 at 9:03
  • Yes i know ng-transclude is for this.But just dont know how to use it for nested divs. Commented Apr 12, 2016 at 9:05

1 Answer 1

1

Use this:

<my-wrapper>
  Your content here
</my-wrapper>

And script (UPDATED: Load html file instead inline html):

module.directive('myWrapper', function ($http, $compile) {
    var linker = function (scope, element, attrs) {

          $http.get('wrapper.html').success(function (data) {
             element.replaceWith($compile(data)(scope));
          });

    };

    return {
        restrict: 'E',
        link: linker
    };
});

JSFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again. btw what should i do if i want to use wrapper.html instead of writing html codes into directive ?

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.