0

This doesn't work of course, but should explain what I want to achieve:

In the factory "myapp.factory('gameService', function($http, $location) {" I load data from json feed "return $http.jsonp('api/game/' + id + '?callback=JSON_CALLBACK')", if the data is not loaded change the templateURL, which was loaded in at first, to 404 page url instead.

var get_data = function (id) {
     return $http.jsonp('api/game/' + id + '?callback=JSON_CALLBACK')
            .then(function(response) {
               //data loaded, do magic
            }, function(response) {
                //data not loaded
                templateUrl: 'views/page_not_found.html'; //<- how to inject this so it works?
            });
};

2 Answers 2

0

The best way to do this is to make a state in route provider to 'page not found', like this:

app.config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html'
        controller: 'MainCtrl',
      })
      .when('/pageNotFound', {
        templateUrl: 'views/page_not_found.html'
      });
});

and in your function make this:

var get_data = function (id) {
    return $http.jsonp('api/game/' + id + '?callback=JSON_CALLBACK')
        .then(function(response) {
           //data loaded, do magic
        }, function(response) {
            //data not loaded
            window.location = '/#/pageNotFound';
        });

};

Sign up to request clarification or add additional context in comments.

2 Comments

This just creates infinite loop.
Infinite loop? Are you try it?
0

Fixed it with ng-switch and ng-include

In the controller, when data is not loaded I added:

$scope.page_not_found = true;

And to the template I added:

<div ng-switch on="page_not_found">
   <div ng-if="page_not_found" ng-include="'views/page_not_found.html'"></div>
</div>

It doesn't really replace the templateURL, everything happens in one .html template. I hide the main content and import the 404 page with ng-include, this solution seems to be faster as well.

Comments

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.