M-S M-S - 3 months ago 21
AngularJS Question

How to add conditions in directive for ng-class angular

I have a 'p' tag inside a directive which needs to show and hide based on response and some click events. I am trying to achieve this using ng-class. I added ng-class on conditional basis, but due to single and double quotes, it is throwing an error.

How to properly assign the quotes for conditional ng-class inside a directive.

ng-class="{'show hide ': $index > 2 && myJSON.count > 4}"


this is what I tried:

angular.module('mydirective').directive('test'),
function() {
return {
restrict: 'E',
scope: {
myJSON: '=myJSON'
},
template: '<p class="content" ng-repeat="abc in myJSON" ng-class="{'show hide ': $index > 2 && myJSON.count > 4}"></p>'
}
}

Answer
angular.module('mydirective').directive('test'),
function() {
    return {
        restrict: 'E',
        scope: {
            myJSON: '=myJSON'
        },
        template: '<p class="content" ng-repeat="abc in myJSON" ng-class="{\'show hide\': $index > 2 && myJSON.count > 4}"></p>'
    }
}