0

here is my code.

.state("dynamic", {
        url: "/:name",
        controller : 'AppHomeCtrl',
        templateUrl: function (params){

            var myURL = params.name + '.html';

            var validPage = true;
            $.ajax({
                url: myURL,
                async: false })
            .done(function(data) {
                console.log(data);
                if (data.toLowerCase().indexOf("doctype") >= 0) {
                    validPage = false;
                }
            })

            if (!validPage) {
                return 'error.html';
            } else {
                return params.name + '.html';

            }
        },

Is this possible to rewrite a state of a route (using angular-ui-routes) to get a page based on if the page gotten has a doctype or not?

the code above works perfectly. but how can I replace $.ajax with $http I must be missing something...

 $http.get(url).success(function(data) {
 if (data.toLowerCase().indexOf("doctype") >= 0) {
                validPage = false;
            }
 });

do I need to load $http somewhere higher up? etc.

Zuriel
  • 1,848
  • 1
  • 19
  • 32
  • Have you injected `$http`? – snahor Sep 29 '13 at 16:21
  • @snahor `.state` is configured in the `.config` block, and I'm not sure about the availability of services such as `$http` in that block. – M.K. Safi Sep 29 '13 at 16:52
  • Take a look at this: https://github.com/angular-ui/ui-router/wiki#templates. In the last example, `$timeout` is injected and `templateProvider` is used instead of `templateUrl`. – snahor Sep 29 '13 at 17:08

1 Answers1

0

I don't think it's possible to use a service, such as $http, inside the config block.

It seems that what you're trying to do is handle requests for URLs that your application doesn't support. If you were using $routeProvider rather than $stateProvider, it would've been possible to route all unmatched URL requests to .otherwise(), but you can also mimic this behavior while using $stateProvider. See this stackoverflow answer for more details on how to do that.

Community
  • 1
  • 1
M.K. Safi
  • 6,560
  • 10
  • 40
  • 58