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

I'm wondering if simply providing a way to reference to the parent scope (even though it technically isn't a parent) would help. $outerScope or $externalScope or something to indicate it isn't a parent in the inheritance sense.

chrisnicola avatar Aug 07 '13 17:08 chrisnicola

@chrisnicola something like $parent.foo ?

Foxandxss avatar Aug 07 '13 17:08 Foxandxss

+1

sveilleux avatar Aug 08 '13 05:08 sveilleux

This one keeps driving me nuts - I keep running into this and the only way to fix it is to add another div or make the scope not-isolated.

spamdaemon avatar Aug 17 '13 02:08 spamdaemon

Adding the following into the link function solves the problem. It's an extra step for the component creator, but makes the component more intuitive.

    function link($scope, $element, attr, ctrl) {

        if (attr.hasOwnProperty("ngShow")) {
            $scope.$watch("ngShow", function (value) {
                if (value) {
                    $element.show();
                }
                else {
                    $element.hide();
                }
            });
        }

You'll also need to setup scope bindings for ngShow

        scope: {
            ngShow: "="
        }

scottmboring avatar Sep 10 '13 15:09 scottmboring

Other languages use OOP inheritance to solve issues like this, for example, if you had BaseComponent and that had a property called show, then in CustomComponent, which extends BaseComponent, show would already exist. And... you could set the value of show from an external "scope". Maybe directives with isolate scopes should automatically inherit the "ng" attributes unless otherwise specified in the directive. That's how I might solve this problem.

rgraffconnect avatar Oct 17 '13 14:10 rgraffconnect

OH yeah and... +1 ;)

rgraffconnect avatar Oct 17 '13 14:10 rgraffconnect

and another +1

kubino avatar Oct 28 '13 15:10 kubino

Would it be more elegant to have another type of scope declaration that would allow access to isolate scope it it already exists or original scope if it doesn't?

app.directive('MyDir', function(){ return { scope: 'inherit' //This would grab any scope available on the element already }; });

stsvilik avatar Feb 28 '14 20:02 stsvilik