user656449 user656449 - 1 month ago 7
AngularJS Question

hot to do ng-show without attribute

I have bunch of places where I need to add ng-show to the lement to get it hidden. Can it be done from the code at one place?

So instead template:

<tr my-row ng-show="$ctrl.model.toShow()"></tr>


should be:

<tr my-row ></tr>


and then in directive:

function myRow (){
return {
restrict: 'A',
templateUrl: 'my-row.html',
..
link = function(scope,el,attrs){
scope.$watch('modle.toShow', function(){
//something here?
})
}

};
}

Answer

Make these changes to the link function, and this adds an attribute ng-show to your my-row directive.

var app = angular.module('app', [])
.directive('myRow', function($compile) {
    return {
        restrict: 'A',
        templateUrl: 'my-row.html',

        link: function (scope, element, attrs) {
          var attr;
          element.attr('ng-show', true);
          var fn = $compile(element);
          return function(scope){
          fn(scope);
        };

         }
    };
})

HEre is the plunker

Comments