AngularJS:如何取代#39;和amp;撇号和空白

时间:2018-02-25 13:02:46

标签: javascript html angularjs ionic-framework

我收到了RSS新闻源收到的一些数据,需要在HTML中显示离子框架应用程序。有两个问题。我需要将'amp;替换为'(撇号)和空格。

AngularJS:

        $scope.rssItems = [];
        var callbackI = 0;
        for(var i=0; i<$marketProvider[$scope.currentMarket].rss.length; i+=1){
          $webServicesFactory.get($marketProvider[$scope.currentMarket].rss[i], {Accept: "application/xml"}).then(
            function success(xmlData) {
              console.log("Home News: ", x2js.xml_str2json(xmlData));                 
              if (x2js.xml_str2json(xmlData) == null)
                  $ionicLoading.hide();


              $scope.rssItems = $scope.rssItems.concat(x2js.xml_str2json(xmlData).rss.channel.item);
//              $scope.rssItems = $scope.rssItems.replace("amp;",""); 

              callbackI+=1;
              if(callbackI == $marketProvider[$scope.currentMarket].rss.length){
                //console.info($scope.rssItems);
                $ionicLoading.hide();
              }
            },
            function error(error) {
              $ionicLoading.hide();
            }
          );

和html

    <div class="list">
      <h5 class="item item-positive item-divider" align="justify">Today's Headlines</h5>
      <span ng-if="rssItems.length==0">No news available.</span>
      <div class="item item-thumbnail-left" ng-bind-html="rssItems(item)" ng-repeat="item in rssItems | orderBy: -dateOrder" ng-click="openNews(item.link)">
        <img ng-src="{{item.enclosure._url || item.enclosure[0]._url || item.content._url || item.thumbnail._url || 'img/icon.png'}}">
        <h2>{{item.title.replace("amp;","") | htmlToPlaintext}}</h2>
        <p>{{item.pubDate | shortDate}}
        <p>{{item.description | htmlToPlaintext}}</p>
      </div>
  1. <h2>{{item.title.replace("amp;","") | htmlToPlaintext}}</h2>能够正确替换但<h2>{{item.title.replace("&#39;","'") | htmlToPlaintext}}</h2>无法替换为aprotrophe。
  2. 我需要立即更换两个。

1 个答案:

答案 0 :(得分:0)

创建一个自定义过滤器,你会没事的:

<强>&GT; demo fiddle

视图

<div ng-controller="MyCtrl">
  Hello, {{ name  | replaceMe }}!
</div>

AngularJS应用程序

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope) {
    $scope.name = 'My &#39;sweetamp; &#39;sweetamp;';
});

myApp.filter('replaceMe', function () {
  return function (data) {
   return data.replace(new RegExp('amp;', 'g'), "")
              .replace(new RegExp('&#39;', 'g'), "'")
  }
})