AngularJS directive only when the condition is true

前端 未结 3 1616
無奈伤痛
無奈伤痛 2021-01-06 21:50

I am going to have a contextmenu directive in ng-repeat items. Based on whether a condition is true, the directive should be applied. How do I put a condition like only when

3条回答
  •  庸人自扰
    2021-01-06 22:25

    You can do this way

    angularApp.directive('element', function($compile) {
            return {
                restrict: 'E',  
                replace: true,
                transclude: true,
                require: '?ngModel',
                scope: 'isolate',
                link: function($scope, elem, attr, ctrl) {
                    $scope.isTrue = function() {
                        return attr.hasMenu;
                    };
                  if($scope.isTrue())
                    //some html for control
                    elem.html('').show();
                  else
                    //some html for control
                    elem.html('').show(); 
    
                    $compile(elem.contents())($scope);
    
                }
            };
        });
    

提交回复
热议问题