0

This is the first time I've tried using Angular with Wordpress. I want to inject $location into the config module so I can use it like this:

app = angular.module 'app', [ 'ngResource', 'ngRoute' ]
app.config [ '$routeProvider', '$location', ( $routeProvider, $location )->

    $location.hasPrefix '!'
    $location.html5Mode true

]

Unfortunately, using $location or $locationProvider with config is causing an Unknown Provider error. I've included all necessary dependencies ie. angular-route.min.js, however, it's still not resolving properly.

If I remove $location it works.

app = angular.module 'app', [ 'ngResource', 'ngRoute' ]
app.config [ '$routeProvider', ( $routeProvider )->

    # ROUTES 

]

EDIT

If I replace $location with $locationProvider I get Failed to instantiate module app due to: TypeError: Object # has no method 'hasPrefix'

Foreign Object
  • 1,630
  • 11
  • 22

2 Answers2

1

Seems fine if you use the name $locationProvider as the name of the injectable.

Here is a plunker.

And the js:

angular.module('myApp', ['ngResource', 'ngRoute'])
.config(function($routeProvider, $locationProvider) { // provider-injector
    $locationProvider.hasPrefix = '!';
    $locationProvider.html5Mode = true;
  });

For reference from Docs:

Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
  • Hmm, `$location` or `$locationProvider` both work, if I assign them like `$locationProvider.html5Mode = true;`, but not if I use the method `$locationProvider.html5Mode(true);` http://stackoverflow.com/questions/14642634/angular-location-hash-prefix – Foreign Object Mar 06 '14 at 18:27
0

The error was a simple typo.

$location.hasPrefix('!');

should be

$location.hashPrefix('!');

Foreign Object
  • 1,630
  • 11
  • 22