How do I apply an AngularJS directive based on a class set by ng-class?

后端 未结 3 491
情歌与酒
情歌与酒 2020-11-29 06:16

I\'m trying to conditionally apply a directive to an element based on its class.

Here\'s a simple case of my issue, see the results in this fiddle. For this example,

3条回答
  •  误落风尘
    2020-11-29 07:06

    According to me don't use a conditional directive based on a class set by ng-class like this question.

    Don't use this

    This will have the directive applied as I expect
    This will not have the directive applied but I expect it to

    because it is really very complicated to handle in complex situation, so always use conditions in your custom directive.

    Use this

    link: function (scope, element, attrs) {
        scope.$watch('condition', function(condition){
            if(condition){
                // something going here
            }
            else{
                // something going here
            };
        });
    }
    

提交回复
热议问题