1

Below is my tabbed window please suggest me where to provide the javascript for the below menu inside the template of the directive.

enter image description here

below is my directive

app.directive('basictabs', function() {
    return {

  restrict: 'AE',
  replace: true,
 template :'<div class="tab-menu">\
     <ul>\
     <li class="unselected"><a href="#">Tab 01</a></li>\
     <li class="selected"><a href="#">Tab 02</a></li>\
     <li class="unselected"><a href="#">Tab 03</a></li>\
     <li class="unselected"><a href="#">Tab 04</a></li>\
     </ul>\
     <div class="tab-content">\
      <div class="tab-01">\
       Content in Tab 01 goes here.\
      </div>\
      <div class="tab-02">\
       Content in Tab 02 goes here.\
      </div>\
      <div class="tab-03">\
       Content in Tab 03 goes here.\
      </div>\
      <div class="tab-04">\
       Content in Tab 04 goes here.\
      </div>\
   </div>\
   </div>'     
  };
});
2
  • What do you mean ? What JS do you want to provide ? Commented Jul 14, 2014 at 12:24
  • numerous places it could be. Not enough detail given to provide good answer Commented Jul 14, 2014 at 12:31

1 Answer 1

1

Link Function

We use the link option to create a directive that manipulates the DOM. The link function is optional. If the compile function is defined, it returns the link function; therefore, the compile function will overwrite the link function when both are defined. If our directive is simple and doesn’t require setting any additional options, we may return a function from the factory function (callback function) instead of an object. When doing so, this function will be the link function.

The link function has the following signature:

link: function (scope, element, attrs) {

// manipulate the DOM in here

}

scope

The scope to be used by the directive for registering watches from within the directive.

element

The instance element is the element where the directive is to be used. We should only manipulate children of this element in the postLink function, since the children have already been linked.

attrs

The instance attributes are a normalized (pascalCased) list of attributes declared on this element and are shared between all directive linking functions. These are passed as JavaScript objects.

You can use JQuery within the link function for manipulating the DOM like as shown in the example

Working Demo

app.directive('basictabs', function() {
    return {
        restrict: 'AE',
        replace: true,
        template: '<div id="tabs">\
            <ul>\
                <li><a href="#tabs-1">Tab 1</a></li>\
                <li><a href="#tabs-2">Tab 2</a></li>\
                <li><a href="#tabs-3">Tab 3</a></li>\
            </ul>\
            <div id="tabs-1">\
                <p>Content for Tab 1</p>\
            </div>\
            <div id="tabs-2">\
                <p>Content for Tab 2</p>\
            </div>\
            <div id="tabs-3">\
                <p>Content for Tab 3</p>\
            </div>\
          </div>',
        link: function(scope, elm, attrs) {
            var jqueryElm = $(elm[0]);
            $(jqueryElm).tabs()
        }
    };
})
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.