0

My code as follows:

 <div class="arInfo"><p class='article' ng-bind-html="bodyText"></p></div>

js:

$scope.bodyText = <p>In its decisions, <a href="#" ng-click= "open('http://www.facebook.com')">Facebook</a> ;
$scope.open = function(url){
   window.open(url, '__blank');

}

But,when I click here, it will not open a new window.I think it because the $scope did not bind to here.

I try to change my code like this:

$scope.bodyText =  $interpolate(<p>In its decisions, <a href="#" ng-click= "open('http://www.facebook.com')">Facebook</a>)($scope);

and this:

$scope.bodyText =  $compile(<p>In its decisions, <a href="#" ng-click= "open('http://www.facebook.com')">Facebook</a>)($scope);

It also cannot work Anybody knows how to solve this problem?

Miao
  • 159
  • 8
  • this will not work. you cannot attach angular code as string variables without using `$compile`. – Claies Oct 28 '15 at 22:23
  • Actually,I use $compile, it also can not work – Miao Oct 28 '15 at 22:24
  • basically, this isn't how angular was intended to be used. Is there some reason you can't use a template or a directive? – Claies Oct 28 '15 at 22:26
  • you might follow the recommendations from here: http://odetocode.com/blogs/scott/archive/2014/09/10/a-journey-with-trusted-html-in-angularjs.aspx. It recommends a directive to parse the HTML, which includes using `$sce` and `$compile`. – Claies Oct 28 '15 at 22:28
  • I think this helps. Also uses a directive - http://stackoverflow.com/questions/18157305/angularjs-compiling-dynamic-html-strings-from-database – tiblu Oct 28 '15 at 22:34

1 Answers1

1

$scope.bodyText should be eqaul to a string. So your code should be like this:

$scope.bodyText = 
  '<p>In its decisions, <a href="#" ng-click="open('http://www.facebook.com')">Facebook</a>' ;
Ali Saberi
  • 864
  • 1
  • 10
  • 33
  • It also cannot work.I think it because I use the ng-bind-html, but I do not know how to it – Miao Oct 28 '15 at 22:16
  • why are you trying to use window.open instead of puting the whole url inside the href? and and then using target="_blank" attribute? It's less complecated. – Ali Saberi Oct 28 '15 at 22:23
  • Because,this is a hybrid appkication,I need open the link outside the app – Miao Oct 28 '15 at 22:29