4

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.

2 Answers 2

7

Just set the ng-href

<a ng-href="http://myurl/{{section}}/{{page}}">Page Link</a>
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately this isn't an option. I need to hold the url in a string variable and can't have it declared in the template. The url is actually coming from a service that returns an array of urls.
@StephenMarsh -- Looks like you'll probably need watchers then.
2

In the controller, you don't need to use curly braces expressions.

Replace this:

$scope.url = 'http://myurl/{{section}}/{{page}}';

With this:

$scope.url = 'http://myurl/'+$scope.section+'/'+$scope.page;

And to bind it in your template, use:

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

So, you can now watch for any changes.

1 Comment

Can't bind to 'ng-href' since it isn't a known property of 'a'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.