1

I a m a little stuck with a problem, probably some syntax I can't find on the internet.

I created an angular directive that receives a class name as a scope variable. In the tamplate I want to add the given classname and another class as conditional. something like that:

app.directive('MyDirective', function () {
    return {
        restrict: 'E',
        scope: {
            className: '=',
        },
        template: "<div ng-class="className, 'otherClass':{expression}"></div>"
    }
});

Thanks :)

3 Answers 3

3

Assuming className is a property on the scope:

You could make use of array expression with object literal syntax itself.

 <div ... ng-class="[className, {true : 'otherClass'}[expression]]"

or mix it with class

<div ... class="{{className}}" ng-class="{'otherClass': expression}"   
Sign up to request clarification or add additional context in comments.

Comments

0
change your code like this
    app.directive('MyDirective', function () {
    return {
        restrict: 'E',
        scope: {
            className: '=',
        },
        template: "<div ng-class="'className':true, 'otherClass':    {expression}"></div>"
    }
    });

Comments

0

You can use by your scope data with some condition or expression.

<div ng-class="{'{{item.name}}' : item.condition}">

or

<div ng-class="$variableToEvaluate ? '{{item.nameiftrue}}' : '{{item.nameiffalse}}'">

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.