天天看點

AngularJs $scope function and Filter 異同

首先我們看這樣一個執行個體:

// Filter
sl.filter('titleFilter', function() {
    return function(title, id) {
      var url = '/public/article#/' + id;
      return '<a href="' + url + '" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >' + title + '</a>';
    };
  });
           

使用執行個體:(Works good)

<h6 class="news-title" ng-bind="v.title | titleFilter:v.id"></h6>
           

另一執行個體:(Html插入, It doesn't work, WHY?)

$scope.getHtml = function (html) {
      return $sce.trustAsHtml(html);
    };      
<h6 class="news-title" ng-bind-html="getHtml(v.title | titleFilter:v.id))"></h6>
           

轉換思路:

// Controller $scope function
$scope.getTitle = function(title, id) {
      var url = '/public/article#/' + id;
      return '<a href="' + url + '" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >' + title + '</a>';
    }      

 (Work perfect)

<h6 class="news-title" ng-bind-html="getHtml(getTitle(v.title, v.id))"></h6>