Stanfrancisco
Stanfrancisco

Reputation: 352

Passing string to Angular expression from app.js

I've got this function which redirects to the login page when certain conditions are met. This part is working fine, but I want a message on the login page saying why they were redirected.

Currently I have this function within my app.js

app.run(function ($rootScope, $window) {
    $rootScope.$on('$routeChangeStart', function (event) {
        //stuff to evaluate
        if (stuff = true) {
            $rootScope.$evalAsync(function () {
                $scope.message = "test";
                $window.location.assign('#/login');
            });
        }
    });
});

And I also have a page, login.html, with a div where I want my message.

        <div ng-hide="message == ''" class="alert alert-danger">
            {{message}}
        </div>

I have tried adding $scope to the function, and accessing $scope.message, but I don't think I'm in the correct scope. The function never hits when I include this, but it does without. Is there any way I can access the scope of this login.html page? The login page does have it's own controller, but I need to be able to redirect to there from this app.js page.

Any help is appreciated!

Upvotes: 0

Views: 50

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222532

$scope gets re instantiated (recreated) once you navigate to the new state login, instead of using $scope, you should user service/root$scope/localStorage to pass the value to the next page.

Change it using $rootScope as,

 $rootScope.$evalAsync(function () {
         $rootScope.message = "test";
         $window.location.assign('#/login');
 });

Upvotes: 1

Related Questions