Coo Coo - 1 month ago 6
AngularJS Question

$state.href with absolute: true doesn't return the right url

I'm using Angular and UI router. I'm trying to get a link shown on the page that the user can copy and share. This thread has shown me that

$state.href
is the function I'm looking for, however it isn't generating the correct link.

An important detail here is that the root of my application is not the root of the domain. In this case, the domain is
localhost
, but the root of the angular app is in
localhost/dev/app/
.

Here's the command I'm using inside my controller.

$scope.url = $state.href('survey', { survey: "asd" }, {absolute: true});


In my app.js, the following route is declared:

.state('survey', {
url: "/:survey/survey?ao",
templateUrl: "views/survey/survey.html",
controller: "surveyController",
},
data: {
requireLogin: false,
requireAdmin: false
}})


This should return
http://localhost/dev/app/#/asd/survey
, instead it returns
http://localhost/#/asd/survey
.

(The remarkable thing is that
ui-sref="survey({survey: "asd"})
does translate to the correct link.)

Is there a way I can fix this so I get the full url?

Answer

Adding a base tag to my app page solved this issue for me. It defines the base URL for the page with the router references. For legacy support reasons, I'm on version 0.2.15 of Angular UI Router. I don't know if this is still necessary for more current versions.

<base href="http://localhost/dev/app/" />
Comments