1

I have a angularjs app with a controller and a partial wired up..

In my controller I have a array of links..

$scope.links = ['http://www.example.com/1','http://www.example.com/2'];

In my partial, I have the following code..

 <div ng-repeat="link in links">
 <a href="{{link}}" target="_blank">Link</a>
 </div>

This does not seem to work.. I am running this via a NodeJS app locally..and so my URLs always end up as http://dev-server.local:3000/"http://www.example.com"

Can anyone please help me figure out how I can add a hyperlink from my controller directly into my partial template and make Angular not append the page URL..

3
  • did you use the <base> tag? Commented Feb 13, 2014 at 12:02
  • no i havent used the <base> tag Commented Feb 13, 2014 at 12:08
  • I am not sure of the usage of <base> tag.. can you please assist.. Commented Feb 13, 2014 at 14:22

2 Answers 2

6

You have to explicitly trust extern URL:s. Look at the documentation for $sce.

In you controller, make sure you have a dependency to $sce, then in create a method that trust the external url.

$scope.trustUrl = function(url) {
    return $sce.trustAsResourceUrl(url);
}

In your view you can reference this method and pass in the url with

<a ng-href="{{ trustUrl(item) }}">Click me!</a>
Sign up to request clarification or add additional context in comments.

Comments

4

instead of

href={{link}}

use

ng-href={{link}}

1 Comment

This is correct.. but there was a problem in my JSON parsing as well.. and that was also the reason it was failing..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.