Can I have Angular ng-href bind variables within a string

Stephen Marsh picture Stephen Marsh · Jan 22, 2015 · Viewed 17.3k times · Source

I'd like to bind a links href property to a variable in my controller but I'd also like to have that url be bound to variables. I'd like to accomplish this using the built in binding and without having to manually watch for changes and reload the url. Is this possible?

// In the controller
$scope.section = 'section1';
$scope.page = 'page1';
$scope.url = 'http://myurl/{{section}}/{{page}}';


<!-- In the template -->
<a ng-href="{{url}}">Page Link</a>

This is a simplification of my actual code. Declaring the url pattern in the template would work but I need to have the url be defined in a string that gets passed in.

Answer

tymeJV picture tymeJV · Jan 22, 2015

Just set the ng-href

<a ng-href="http://myurl/{{section}}/{{page}}">Page Link</a>