firebolt_ash
firebolt_ash

Reputation: 1324

Angular js ng show not working

I am working on a small web app and facing an issue.

I want to show a preloader svg inside a div as below :-

<div class="loader" ng-show="main.showLoader">
<svg class="spinner" width="65px" height="65px" viewBox="0 0 66 66" xmlns="http://www.w3.org/2000/svg">
    <circle class="path" fill="none" stroke-width="6" stroke-linecap="round" cx="33" cy="33" r="30"></circle>
</svg>

In my HTML I switched main.showLoader = true on click of a button.

Controller Function :-

$scope.setCA=function(cont,click){

    if($scope.aC!=null){

        $scope.aC={};                           
    }
        if($scope.c[c.p]==null){ 
            $scope.mC='';

            .....Server interactions here 


                var chat={
                    'name':cont.name,
                    'phone':cont.phone,
                    'msgs':msgs,
                    'unreadMsgs':unreads,
                };

                $scope.chats[cont.phone]=chat;                  //Assign the messages from repective users in the chats array to the respective contact phone number as here                        
                $scope.$apply(function(){
                    $scope.activeChats[cont.phone]=chat;
                    if(click)
                        $scope.setChatInFocus($scope.chats[cont.phone]);        //Send chats to setChatinFocus       
                });
            });
        }
        else{
            $scope.showFirstTime = false;
            if($scope.activeChats[cont.phone]==null){
                $scope.activeChats[cont.phone]=$scope.chats[cont.phone];
            }
            if(click)
                $scope.setChatInFocus($scope.chats[cont.phone]);    
        }
        setTimeout(function(){scrollToBottom()},10);
         this.showLoader=false;
    };

HTML:-

<div class="singleContact person" ng-cloak ng-class="{active: contactName === contact.name}" 
 ng-repeat="contact in contacts"
 ng-click='main.showLoader=true;setChatActive({ "name": contact.name, "phone": contact.phone },true)>

It switches on as intended but does not go off. Some help ?

Upvotes: 0

Views: 110

Answers (2)

Konstantin A. Magg
Konstantin A. Magg

Reputation: 1118

I think the properties are placed on different objects. When you enable the loader, the code sets $scope.main.showLoader to true. The timeout callback on the other hand sets a variable this.showLoader to false. As @tpie already explained, this in this case only references to the timeout callback function itself.

The HTML snippet you posted is not complete, so I can only guess. Using the following line to reset showLoader can solve the problem.

$scope.main.showLoader=false;

Upvotes: 0

tpie
tpie

Reputation: 6221

Try doing var self = this; at the top of your controller and change it to self.showLoader = false

In the current context, this is the function you are in.

Upvotes: 1

Related Questions