user656449
user656449

Reputation: 3032

hot to do ng-show without an attribute

I have bunch of places where I need to add ng-show to the element to hide it. Can it be done from one place in the code?

So instead of this template:

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

It should be:

<tr my-row ></tr>

and then in the directive:

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

    };
}

Upvotes: 0

Views: 46

Answers (1)

Sravan
Sravan

Reputation: 18657

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

Upvotes: 1

Related Questions