简体   繁体   English

如何在指令中为ng级角添加条件

[英]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. 我在指令中有一个“ p”标记,该指令需要根据响应和一些单击事件来显示和隐藏。 I am trying to achieve this using ng-class. 我正在尝试使用ng-class实现此目的。 I added ng-class on conditional basis, but due to single and double quotes, it is throwing an error. 我在有条件的基础上添加了ng-class,但是由于单引号和双引号引起了错误。

How to properly assign the quotes for conditional ng-class inside a directive. 如何在指令中为条件ng-class正确分配引号。

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>'
        }
    }

A good option will be to use the templateUrl and move the HTML code to separate file. 一个不错的选择是使用templateUrl并将HTML代码移到单独的文件中。 This will get ride of the quotes issue. 这将解决报价问题。

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>'
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM