1

First, I found this answer, but they do not suit..

My problem is, that I want my AngularJS part of my application to reload something, when the user is redirected back to the page. The login flow is like this

a.php -> [FB Magic] -> callback.php -> a.php

My problem is <body onload="foo()"> fires only once. The same is true for DOMContentLoaded and window.onload as well.

For a.php it is like the user never left the page.

How can I make AngularJS call a function (which is defined within a controller), when the user is redirected back? (I am using PHP for redicting back

EDIT: How my controller looks like ->

pwm.controller('EventListController', ['$scope', 'pwmApi',
function EventListController($scope, pwmApi) {
    $scope.fetched = false;
    $scope.events = [];
    let update = function () {
       <!-- Does stuff, is a bit long ->
    };
    update();
}]);

So :) The update(); method is called on Construction of the controller. When I am redirected back the update-function is NOT called - Refreshing the page helps.

I sincerly hope this clarification helped. Reloading the page after getting redirected back helps, but now nothing happens...

Community
  • 1
  • 1
Florian Reisinger
  • 2,638
  • 4
  • 23
  • 34

3 Answers3

0

You can make $url like this - $url = "abc?rediect='true';

here redirect='true' means you are redirecting.

In your angularJs controller do this-

Controller

.controller('controller',function($location){
   var queryParameters = $location.search();

   // Check if redirect is true
   if(queryParameters.redirect === 'true')}{
      // Call a function
      myFunaction();
   }
});

Update - in query string all values will be sting so boolean true will also become 'true' so you have to handle that like a string not boolean.

Amit Sirohiya
  • 333
  • 1
  • 13
0

I found a way to do it in PHP (https://stackoverflow.com/a/8577805/1870799)

Instead of header("Location: $url"); (was my original redirect solution) and header("Location: $url?redirect=true"); I am now using header("refresh: 0;url=$url"); and it works as expected.

But still... Is it possible with AngularJS to do the same with the Location header?

Community
  • 1
  • 1
Florian Reisinger
  • 2,638
  • 4
  • 23
  • 34
-2

Listening onload doesn't fit well with angular.

If you're using routing with ngRoute or ui-router or even ng-controller, the associated controller will get executed again. If it's not enough, you can listen to the events provided by the routing modules : $routeChangeSuccess/$stateChangeSuccess

Walfrat
  • 5,363
  • 1
  • 16
  • 35