Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

ng-show, ng-hide not working in directive template #2339

Closed
OHB opened this issue Apr 9, 2013 · 3 comments
Closed

ng-show, ng-hide not working in directive template #2339

OHB opened this issue Apr 9, 2013 · 3 comments

Comments

@OHB
Copy link

OHB commented Apr 9, 2013

Hello! I'm trying to make a reusable spinner directive so I can hide sections of the page with a spinner before data is loaded:

myModule.directive('spinner', function() {
    return {
        template: '<div ng-hide="{{variable}}">SPINNER!!!</div><div ng-show="{{variable}}" ng-transclude></div>',
        replace: false,
        transclude: true,
        controller: ['$scope', '$element', '$attrs', function($scope, $element, $attrs) {
            $scope.variable = $attrs.spinner;
        }]
    };
});

I attach it like this:

Angular turns it into:

<div ng-controller="careerStatsCtrl" class="sidebar ng-scope" spinner="careerStats">
    <div ng-hide="careerStats" style="">SPINNER!!!</div><div ng-show="careerStats" ng-transclude="" style="display: none;">
    <!--contents-->
    </div>
</div>

Show/Hide just doesn't work...and I know that careerStats is being set in the careerStatsCtrl controller. If I add the following in the html directly, it works:

<div ng-show="careerStats">SHOW</div><div ng-hide="careerStats">HIDE</div>

Anyone have an idea?

@OHB OHB closed this as completed Apr 9, 2013
@OHB
Copy link
Author

OHB commented Apr 9, 2013

I found an alternate method to do this since I couldn't figure out the initial problem.

myApp.directive('spinner', function() {
    return {
        restrict: 'A',
        controller: ['$scope', '$element', '$attrs', function($scope, $element, $attrs) {
            var spinner = $('<div class="spinnerContainer"><div class="spinner"></div></div>').insertAfter($element);
            spinner.hide().fadeIn("slow");
            $element.hide();
            $scope.$watch($attrs.spinner, function(data) {
                if (data) {
                    spinner.fadeOut("slow");
                    $element.fadeIn("slow");
                } else {
                    spinner.fadeIn("slow");
                    $element.fadeOut("slow");
                }
            });
        }]
    };
});

Use spinner="scopeVariableToWatchForTruthiness" on the element you want to spinnify.

@newtriks
Copy link

Show/hide should indeed work, my thoughts are its simply down to your incorrect use of curly-brace bindings i.e. {{variable}}. Remove those and it should work fine: ng-show="variable"

@gregkerzhner
Copy link

This is much, much later but I think this is related to this: #1459

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants