Javascript 在指令中动态添加ng if属性

Javascript 在指令中动态添加ng if属性,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我使用以下指令向HTML元素添加属性: module1.directive('rhVisibleFor', function ($rootScope) { return{ priority: 10000, restrict: 'A', compile: function (el, attr) { el.removeAttr('rh-visible-for');

我使用以下指令向HTML元素添加属性:

    module1.directive('rhVisibleFor', function ($rootScope) {
        return{
            priority: 10000,
            restrict: 'A',
            compile: function (el, attr) {
                el.removeAttr('rh-visible-for'); 
                el.attr('ng-if', '$layoutPreferences.visibilities[\''+attr.rhVisibleFor+'\']');
//                el.attr('ng-if', 'false');
                var fn = $compile(el);
                return function(scope){
                    fn(scope);
                };
            }

        }
    })

编译成功执行,但生成的代码不响应更改。即使已显示ng if属性,我也显式将其设置为false。

我的问题已解决,问题的原因是指令依赖项中缺少
$scope

module1.directive('rhVisibleFor', function ($rootScope,$compile) ...

请提供plnkr或JSFIDLE