Http.get not work in beforeEnter


#1

I want to get an external json before the view is visible , but $ http.get not operate until this visible sight

$scope.$on('$ionicView.beforeEnter', function () {

                var value = window.localStorage.getItem("url_id");
                var values = value.split("//");
                $scope.url_id = values[1];
                $http.get(CategoriaDatos.local + $scope.url_id).then(function (resp) {
                    $scope.local = resp.data;
                }, function (err) {
                    console.error('ERR', err);
                    // err.status will contain the status code
                })
                 })

;


#2

Have you injected $http into wherever you are calling that code?

I would also suggest it’s better to use a resolve on your route instead as this will only load the view once the file has been successfully loaded, which is more appropriate for what you are doing there.

More info about resolve is here: https://github.com/angular-ui/ui-router/wiki#resolve


#3

I usually just wrap the content I’m going to show in a span with an ng-if that is set to true when the petition is done and I show a spinner while it’s set to false so it’s not just a blank page.


#4

resolves are a more suitable approach when trying to do it between views.


#5

to solve my problem tube to make the request in a previous driver and save localStorage , but I think is not what I want