使用angularjs

时间:2017-05-02 11:56:36

标签: html css angularjs

如果字符串包含" http://"我需要添加锚标记。在里面, ex json回复:

var res =  {status: "ok",activities: [{content: "The icon Canadian. Credit: http://allrecipes.co..."},{content: "The test message"},{content: "The Canadian art http://allrecipes.co"}]}
$scope.posts = res.activities;

但是,我需要显示http的锚标记

我尝试过使用

<div ng-repeat="post in posts">
<span ng-bind="post.content"></span>
</div>

1 个答案:

答案 0 :(得分:0)

你可以做的是将html标签作为字符串添加到内部并使用ng-bind-html将字符串cmpile到dom元素

{content: "The icon Canadian. Credit: <a href='http://allrecipes.co...'> http://allrecipes.co... </a>"}

在html中

 <span ng-bind-html="trust(post.content)"></span>

最后添加此功能

 $scope.trust = function(item){
      return $sce.trustAsHtml(item)
 }

&#13;
&#13;
angular.module("app",[])
.controller("ctrl",function($scope,$sce){

var res =  {status: "ok",activities: [{content: "The icon Canadian. Credit: <a href='http://allrecipes.co...'> http://allrecipes.co... </a>"},{content: "The test message"},{content: "The Canadian art <a href='http://allrecipes.co'> http://allrecipes.co </a>"}]}
$scope.posts = res.activities;

$scope.trust = function(item){
  return $sce.trustAsHtml(item)
}
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <div ng-repeat="post in posts">
<span ng-bind-html="trust(post.content)"></span>
</div>
</div>
&#13;
&#13;
&#13;