angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

ngShow in directive with isolate scope is never shown

Open arunjitsingh opened this issue 13 years ago • 29 comments

In a directive with an isolate scope with ng-show="someVar" (someVar from a parent ng-controller's $scope), the directive's tag is never shown ("display: none" isn't removed) (example). Other directives (like ng-repeat) bind values correctly in custom directives with isolate scope.

arunjitsingh avatar Apr 24 '13 15:04 arunjitsingh

If I had to guess exactly the problem it's because you have an isolate scope so it expects that ng-show will be bound inside the directive template and not outside of it.

So as a quick-fix the following works if you put it inside your directive template instead:

template: '<div class="item" ng-transclude ng-show="list.length"></div>'

However the whole point of the isolate scope is to isolate yourself from any direct bindings to the parent scope and instead pass them in. So arguably you should have something more like:

scope: { show: '=' }
template: '<div class="item" ng-transclude ng-show="show"></div>'

// And in the HTML call it like this:

<div my-directive show="list.length">

As for why ng-repeat is working fine, if I had to hazard another guess it is because it is part of the transclusion step. Since you are transcluding inside myDirective it needs to determining what content to transclude in advance. Thus ng-repeat is not happening inside the directive scope but outside of it before being injected into the template through the transclusion step.

I do agree the behaviour is a bit non-intuitive, and I wonder if it wouldn't be better to ensure that binding to the parent scope always "just worked" regardless of an isolate scope or not. As a rule of thumb I try to design my directives to be completely ignorante (isolated) from the controller/scope that contains them. If necessary I'll write a controller inside the directive itself to manage the internal state of the directive.

chrisnicola avatar Apr 30 '13 19:04 chrisnicola

Agree, especially when you have multiple nested components, where you have to declare the "show" property on each one of them...

So maybe when angular see an element directives with attribute directives, it should transclude just them..

Something like:

<my-panel ng-show="someVar"></my-panel>

Before my-panel's template is compiled, it should transclude the other attributes on the HTML declaration.

Is that possible?

shairez avatar May 15 '13 10:05 shairez

"I do agree the behaviour is a bit non-intuitive" Me too, that dont feels right. I understand the technical background, but for from the developers point of view it should be possible.

There is also a Google Group for this: https://groups.google.com/forum/#!msg/angular/FqvC0ciG08w/k7KkyJu7zNoJ

Lets find a solution...

robinboehm avatar Jun 03 '13 07:06 robinboehm

@shairez Unfortunately in that case, you have to wrap it in a div which has the ng-show directive.

0x-r4bbit avatar Jun 03 '13 07:06 0x-r4bbit

+1 to support ng-show on custom directives with isolated scope

tam4s avatar Jun 13 '13 14:06 tam4s

+1 It would be helpful somewhat declare inside directive statement that you want to use ng-show ng-hide on it.

gaevoy avatar Jun 15 '13 10:06 gaevoy

+1

jrencz avatar Jun 23 '13 23:06 jrencz

+1; I just ran into this.

keir avatar Jun 24 '13 18:06 keir

+1... I don't think anybody placing attribute expressions on an element directive (at the point of use) is wanting them to run in the context of the isolate scope.

kevinsbennett avatar Jun 26 '13 00:06 kevinsbennett

+1

jyboudreau avatar Jul 09 '13 18:07 jyboudreau

+1

amorphius avatar Jul 10 '13 19:07 amorphius

+1

jim-cooper avatar Jul 10 '13 22:07 jim-cooper

+1

rob-balfre avatar Jul 12 '13 09:07 rob-balfre

seems unnecessary to mention but i also would intuitively expect the isolate scope not to affect the other directives

scamden avatar Jul 12 '13 20:07 scamden

:+1:

arvearve avatar Jul 15 '13 18:07 arvearve

+1

chiefy avatar Jul 23 '13 17:07 chiefy

:+1:

devmondo avatar Jul 23 '13 23:07 devmondo

I agree with all of you, but if we had an isolated scope per attribute instead of the whole element, I am not sure of how easy would be having directives with options (via other attributes).

I think that trying to do the hell of workarounds just for avoiding an extra div is overkill.

Foxandxss avatar Aug 07 '13 09:08 Foxandxss

But it's not just to avoid an extra div, how about trying to set focus into an input element that has type ahead. It's really hard to do it not hackily if you're using isolate scope. I guess the point is this affects more than just Ng-show — Sent from Mailbox for iPhone

On Wed, Aug 7, 2013 at 2:36 AM, Foxandxss [email protected] wrote:

I agree with all of you, but if we had an isolated scope per attribute instead of the whole element, I am not sure of how easy would be having directives with options (via other attributes).

I think that trying to do the hell of workarounds just for avoiding an extra div is overkill.

Reply to this email directly or view it on GitHub: https://github.com/angular/angular.js/issues/2500#issuecomment-22239831

scamden avatar Aug 07 '13 16:08 scamden

Ah!, you got me :+1:

Foxandxss avatar Aug 07 '13 16:08 Foxandxss