0

I'm trying to customize a couple tabs because they're different. Here's what I have:

<md-tabs>
    <md-tab ng-repeat="tab in tabs" ng-class="tab.customClass">
        <md-tab-label ng-bind="tab.label"></md-tab-label>
    </md-tab>
</md-tabs>

My issue: the custom class is not in the compiled md-tab-item

NOTE: the gets replaced because it's only needed to generate tab buttons and panes.

I don't know how many tabs I have, so I cannot write CSS based on position.

Any ideas?

1 Answer 1

1

If you are trying to customize how the tab itself looks at the top you can do this through the use of a decorator. This will allow you to alter how directives behave at run time.

For example if you are trying to style the tab itself, that directive would be "md-tab-item"

(function () {
'use strict';

MdTabItemDecorator.$inject = ['$provide'];

angular.module('common').config(MdTabItemDecorator);

function MdTabItemDecorator($provide) {
    $provide.decorator('mdTabItemDirective', [
        '$delegate',
        '$controller',
        function ($delegate) {

            var directive = $delegate[0];

            directive.compile = function () {
                return function (scope, elem, attrs) {
                    directive.link.apply(this, arguments);
                    elem.attr('style', 'color:red');
                    scope.tabIndex = scope.$parent.$index;
                };
            };

            return $delegate;
        }
    ])
}

})();

The above example would change the color of the tab text to red.

What's happening here is that we are creating a decorator mdTabItem with

function MdTabItemDecorator($provide) {
$provide.decorator('mdTabItemDirective', [
    function ($delegate) {

      ...

    }
]) 

This gives you access to the $delegate object which is a representation of the directive object that is about to be instantiated.

This allows us to make some modifications and return the delegate object changing how the directive behaves.

In this case I am taking the existing link function and extending it's functionality to set the style of the directive element.

        directive.compile = function () {
            return function (scope, elem, attrs) {
                directive.link.apply(this, arguments);
                elem.attr('style', 'color:red');
                scope.tabIndex = scope.$parent.$index;
            };
        };
Sign up to request clarification or add additional context in comments.

3 Comments

Yep. I figured it out yesterday! Luckily I was running AngularJs 1.5. Actually I had to do something different, because the scope of the first repeat goes lost. I'll answer the question as soon as I can...
The above will work in that case, It changes how the directive behaves in all cases.
Yes. But my request was more specific. I wanted to add a custom class to the generated tabItem. Your example can only add the same style for all the tabItems. Also in the last line => scope.$parent.$index is undefined. Because there's no repeat in the parent scope. Cheers.

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.