2

I got some data received by an RSS news feed and need to display in in HTML for an ionic framework app. There are two problems. I need to replace ' and amp; with ' (apostrophe) and blank space.

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();
            }
          );

and 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> able to replace correctly but <h2>{{item.title.replace("&#39;","'") | htmlToPlaintext}}</h2> unable to replace with aprotrophe.
  2. I need to replace both at once.
lin
  • 17,956
  • 4
  • 59
  • 83
lotteryman
  • 389
  • 1
  • 6
  • 21

1 Answers1

0

Create a custom filter and you will be fine:

> demo fiddle

View

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

AngularJS Application

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'), "'")
  }
})
lin
  • 17,956
  • 4
  • 59
  • 83
  • sorry for late feedback. It is working partially. It replace only the first ''` but not all. Also, I am calling like 30 news rss at one time, so , I need to replace all if that happened – lotteryman Mar 15 '18 at 07:41
  • How about using `return data.replace(/'/g, "'").replace(/amp;/g, "")`? is it the same? – lotteryman Mar 15 '18 at 08:56
  • @lotteryman yea its the same. But I recommend to create a RegEx by using "RegExp()" function. – lin Mar 15 '18 at 08:58