我收到了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>
<h2>{{item.title.replace("amp;","") | htmlToPlaintext}}</h2>
能够正确替换但<h2>{{item.title.replace("'","'") | htmlToPlaintext}}</h2>
无法替换为aprotrophe。答案 0 :(得分:0)
创建一个自定义过滤器,你会没事的:
<强>&GT; demo fiddle 强>
<div ng-controller="MyCtrl">
Hello, {{ name | replaceMe }}!
</div>
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', function ($scope) {
$scope.name = 'My 'sweetamp; 'sweetamp;';
});
myApp.filter('replaceMe', function () {
return function (data) {
return data.replace(new RegExp('amp;', 'g'), "")
.replace(new RegExp(''', 'g'), "'")
}
})