0

Unable to get the value of $scope when in function in angular js

Here how it looks like

module.controller('MainController', function($scope, $window, $rootScope, checkConnection) {
    $scope.online = false;
    $scope.deviceReady = false;
    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {
        checkConnection.checkCon($scope);
        $scope.checkInetNetwork = $rootScope.checkInetNetwork;

        if ($rootScope.checkInetNetwork == "true") {
            $scope.online = true;
        } else {
            $scope.online = false;
        }
    }
});

I am not able to figure out why <p ng-show="online">ThAnk You Thank you</p> why this is not displaying. When even i am making it TRUE. Out side this function it works like charm.

Any solution?

3 Answers 3

2

because you are changing the scope value outside of the angular digest cycle.

You can call $apply to run the digest cycle to fix it

function onDeviceReady() {
    checkConnection.checkCon($scope);
    $scope.checkInetNetwork = $rootScope.checkInetNetwork;

    if ($rootScope.checkInetNetwork == "true") {
        $scope.online = true;
    } else {
        $scope.online = false;
    }

    $scope.$apply();
}

function onDeviceReady() {
    checkConnection.checkCon($scope);
    $scope.checkInetNetwork = $rootScope.checkInetNetwork;

    $scope.online = $rootScope.checkInetNetwork == "true";

    $scope.$apply();
}
Sign up to request clarification or add additional context in comments.

Comments

1

You'll need to run a $scope.$apply as something has happened outside of Angular's knowledge.

function onDeviceReady() {
    checkConnection.checkCon($scope);
    $scope.checkInetNetwork = $rootScope.checkInetNetwork;

    if($rootScope.checkInetNetwork == "true"){
        $scope.online = true;
    }
    else
    {
        $scope.online = false;
    }
    $scope.$apply();
}

Comments

0

Use $rootScope.online = false instead of $scope.online = false so you can have an access of the $scope everywhere you want to call this.

2 Comments

No this method is not workable. But yeah thank you so much for support
Sorry, I thought that you can't have access to your $scope! :) You're welcome! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.