json2031 json2031 - 3 months ago 19
AngularJS Question

How can I pass url params to templateUrl

I am using angular ui-router to retrieve a page, but on the documentation, it only shows how to pass in parameters in the url, not the template url.

Is there anyway where it can be passed in the templateUrl?

Below is my code

<div ui-view></div>
<!-- We'll also add some navigation: -->
<a ui-sref="contacts({contactId: 1})">State 1</a>


And the angular config:

myApp.config(function($stateProvider) {
$stateProvider
.state('contacts', {
url: "/states?myParam={contactId}",
templateUrl: "states1.html"
});
});

Answer

templateUrl can be a function that takes stateParams parameter:

.state('contacts', {
    url: "/states?myParam={contactId}",
    templateUrl: function(stateParams) {
        return "states" + stateParams.contactId + ".html";
    }
})
Comments