I am calling an OAuth Process in the device ready function using InAppBrowser in app.js where i am getting a token and upon checking of that token i am redirecting to a particular view.
.run(function($ionicPlatform,$rootScope,$http,jwtHelper,$cordovaInAppBrowser,$state,jwtHelper) {
$ionicPlatform.ready(function() {
if(isValidUser)
{
$state.go('app.playlists');
window.location.reload(true);
}
}
Now the window.location.reload is redirecting to the view but it is falling inside an infinite loop. If i only write $state.go, it is not redirecting to the view.
Also, when i put
$urlRouterProvider.otherwise('/app/playlists');
The above view gets fired for 2 secs and then my oauth login screen gets fired(Inside inappbrowser). So, i can't use that.
How to fix it.
You may use $location.path instead of $state.go:
$location.path('/app/playlists');
Note: Don't forget to inject $location.
Related
I am trying to logout a user using a JavaScript code but it keeps loading without logging out.What have I done wrong?
I have tried changing the function itself but it still wouldn't log out.The loader just keeps on running.
this is the code
logout.html(loader);
$.get("modules/"+role+"/"+role+".php",{
},function(pagedata){
logout.show().html(pagedata);
});
I'm expecting the code to logout and take me back to the login page.
The
$.get()
method requests data from the server with an HTTP GET request.
The required URL parameter specifies the URL you wish to request.
The optional callback parameter is the name of a function to be executed if the request succeeds.
$.get(URL,callback);
so change your code :
logout.html(loader);
$.get("modules/"+role+"/"+role+".php",function(pagedata){
logout.show().html(pagedata);
});
I'm using a code like this to create an account and than i want to validate (with some specifics functions) and after that redirect to the main page.
$scope.createAccount = function(cad) {
$http.post('dist/func.php?action=createAccount', cad).then(
function(res) {
//..more functions
$window.location.href = '/adm';
},
function(err) {
Notification.error(feedbackError);
}
);
};
The problem is, when the user arrive in the new page, the mainCtrl is not running (the page is not fully reloading) so i can't get essential data for this new user.
I saw some other similar questions, but all of them says we should use $window.location.href instead of $location to get a full page reload. But until now, i had no success with it.
How can i solve it?
All,
I have an AngularJS PhoneGap app I am working on. I am stuck on one redirect that occurs immediately after receiving an API call response. My code is located at gist.github.com
My issue is on line 300 of controllers.js, where I am calling my redirect within a then() block.
UserService.login($scope.user.email, $scope.user.password)
.then(function(response) {
globals.session_id = response.data.meta.session_id;
globals.logged_in_userId = response.data.response.users[0].id;
if ($scope.user.remember === true) {
window.localStorage.setItem("_session_id", response.data.meta.session_id);
window.localStorage.setItem("logged_in_userId", response.data.response.users[0].id);
} else {
window.localStorage.removeItem("_session_id");
window.localStorage.removeItem("logged_in_userId");
}
})
.then(function() {
$location.path("/tab/locations");// <=== This line doesn't cause an error but does not result in a redirect
})
.
catch (function(response) {
alert(JSON.stringify(response));
});
If anyone could please help me out, I'd appreciate it!
Thanks,
Bruce
Try $rootScope.$apply(); after $location.path()
OR
$scope.$apply(function() {
$location.path("/tab/locations");
});
You can also use $location.url()
Your then handler needs to return something so that the promise will be stilled resolve for the next condition.
Just add return response at the end of your then handlers.
It appears the issue was actually in my app.js run function call. On location change it looks at a global variable to see if a username has been set yet, and if not, it intercepts the call to send the client to the login page. Once I moved my code inside the OnLocationChange handler to look at the variable there, it redirected properly.
Thanks,
B
Working with Angular and im running into a few issues using $location.url & $location.path
The URL changes at the top of the page to /users/sign_in but I don't see the view until I manually refresh the page, then it appears?
function error(response) {
if (response.status == 401) {
$rootScope.$broadcast('event:unauthorized');
$location.url('/users/sign_in');
};
};
I am not getting any errors.
You're doing a bit too much in your function. You're attempting to change the URL but also return a response. You can try using a $scope.$apply() right after the $location.url call, however you should consider splitting this logic so that the redirect occurs based on the returned response, or don't return any response from the error function.
I have been using AngularJs with Restangular with PHP backend.
I am calling a function based on route (detail/list) to fetch into variable from Restangular.
The problem is if there is a delay in database connection the page is displayed blank and after a few(2-3) seconds the data is filled and shown,
The issue repeats when navigating to details and going back to the list page.
Here is an example how you can resolve your data before the page is rendered (w/o blank screen):
The 'resolve' function is called before the view is rendered.
$routeProvider.when('/admin/task/:id' , {
templateUrl: '/app/task/admin-task-results.tpl.html',
controller:'AdminTaskDetailsCtrl',
resolve: {
task: function($route, Restangular) {
return Restangular.one('tasks', $route.current.params.id).get();
}
}
});
Just pass the resolved object (task in this example) as argument into your controller:
module.controller('AdminTaskDetailsCtrl', function ($scope, task) { ... });
However, let me point out that this approach is not always the best solution, because the user does not get immediate feedback after changing the route. Consider loading your data in the controller and display a spinner until the data arrived.
I assume that you still need to wait for data loaded, so to draw view without the data and load date later you can use next in the controller constructor:
$scope.$on('$viewContentLoaded', function(){
// Load data into model here
});