3

I tried to load the directive as below but it is not working.

<div ng-class="{'nav-dropdown' : dt.url == null }"> </div>

It works fine if I just assigned it to a class like this:

<div class="nav-dropdown"> </div>

EDIT:

When using this method:

<div ng-class="{'nav-dropdown' : dt.url == null }"> </div>

The class is added inside html as

<div class="nav-dropdown"> </div>

The main problem is : the directive doesn't show up as

 <div class="nav-dropdown"><div>This is directive template</div></div>

What I want to do is load the directive with a condition: dt.url is null. Anyone can help?

2
  • dt is data. I load it from JSON data. I check if the url is null, then apply the class "nav-dropdown" for calling the directive to be displayed. @BidhanA, do you have any idea? Commented Jun 20, 2015 at 3:29
  • Maybe the url isn't null and so the condition returns false. Have you checked it? Commented Jun 20, 2015 at 3:35

3 Answers 3

2

You can use ternary expression

<ng-class="condition ? 'true' : 'false'"></div>

So, instead use this:

<div ng-class="{'nav-dropdown' : dt.url == null }"> </div>

You could use this: (This works for me)

<div ng-class="dt.url == null ? 'nav-dropdown' : ''"></div>
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest evaluating it as either true or false as it seems that ng-class only evaluates to a boolean, so you should just use:

<div ng-class="{'nav-dropdown' : !dt.url }"> </div>

Comments

0

Edit after OP edit

The directive ng-class is specifically for dynamically setting a CSS class, not a directive. The order in which the directives are evaluated is most likely complicating things (ex: the nav-dropdown directive would need to be compiled after the ng-class directive was evaluated).

To dynamically set a directive, maybe reconsider passing the data to the directive via the scope, and have the directive behave differently if 'url' is null.

Here is an updated fiddle demonstrating this:

http://jsfiddle.net/spanndemic/7Ltm9bfq/

html:

<div ng-app="docsSimpleDirective">
    <div ng-controller="Controller">
        <div nav-dropdown dropdown-data="dt1"></div>
        <div nav-dropdown dropdown-data="dt2"></div>
    </div>
</div>

js:

angular.module('docsSimpleDirective', [])
.controller('Controller', ['$scope', function($scope) {
    $scope.dt1 = {
        url: null
    };
    $scope.dt2 = {
        url: 'http://www.google.com'
    };
}]).directive('navDropdown', function() {

    var _link = function($scope, element, attrs) {
        var el;
        if ($scope.dropdownData.url === null) {
            el = '<div>URL not set</div>';
        } else {
            el = '<div>URL is: ' + $scope.dropdownData.url + '</div>';
        }

        element.replaceWith(el);
    };

    return {
        replace: true,
        link: _link,
        scope: {
            dropdownData: '='
        }
    };
});

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.