AngularJS - help with ng-bind URL

Hi..
I'm trying to bind a var which contains an URL and ng-click.

I'm binding this url - but it doesn't show as a hyperlink.

var myURL = "<a ng-click='formatter.GotoLink('http://www.cnn.com');'>LINK</a>"

 it appears literally ...  like "<a ng-click='formatter.GotoLink('http://www.cnn.com');'>LINK</a>"
instead of "LINK"

 Any ideas what's wrong with myURL string?

 
               
     

    formatter.GotoLink = function (url){
        window.open(link,'_system');
    }
LVL 1
JElsterAsked:
Who is Participating?
 
Ioannis ParaskevopoulosCommented:
You can try the following:

On your view:
<a href="#" ng-click="setUrl('http://www.cnn.com')">Link</a>

Open in new window


and on your controller (you will need to inject $window service):
module.controller('mainCtrl',function($scope, $window){
	$scope.setUrl = function(url){
		$window.location=url;
	}
})

Open in new window


Giannis
0
 
Ioannis ParaskevopoulosCommented:
Hi,

May i suggest a different approach?

From what you have said it seems that you need a way to bind the href attribute of an anchor to something dynamic.

The way to do this is ingecting $sce service in your controller and then use it to trust the url you enter. Here is an example:

module.controller('mainCtrl',function($scope, $sce){
    $scope.myURL = $sce.trustAsResourceUrl('http://www.cnn.com');
})

Open in new window


And then in your view:

    <a  ng-href="{{myURL3}}">LINK</a>

Open in new window


So now you do not need to inject html in your view. The html is there waiting for the proper value to be loaded.

Giannis
0
 
JElsterAuthor Commented:
How can I click on the link and call a function()?
I need to click on the link and pass the URL.
thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.