angular:过滤以替换和完成相对 <a href="/"> 路径
angular: filter to replace and complete relative <a href="/"> paths
我是 angular 的新手,我正在努力替换文本中的相对 href 路径。
为了举例,我有以下 controller/scope/html/filter:
$scope.text = 'Here you can find the best <a href="/shoes/sport">sport shoes</a>, <a href="/shoes/classic">classic shoes</a> and <a href="/socks/sale">socks</a>. If not, you can <a href="http://www.google.com">google</a> it.';
<p ng-bind-html="text | parseUrlFilter"></p>
我试图创建一个过滤器来搜索所有相关 urls 并替换为完整路径 url(例如:http://www.example.com/shoes/sport, http://www.example.com/shoes/classic, http://www.example.com/socks/sale):
app.filter('parseUrlFilter', function() {
var urlPattern1 = 'href="/shoes/';
var urlPattern2 = 'href="/socks/';
return function(text) {
angular.forEach(text.match(urlPattern1), function(url) {
url = url.replace('href="','');
text = text.replace(url, 'http://www.example.com' + url);
});
angular.forEach(text.match(urlPattern2), function(url) {
url = url.replace('href="','');
text = text.replace(url, 'http://www.example.com' + url);
});
return text;
};
})
但是,我的过滤器仅替换每个模式的第一个案例 (http://www.example.com/shoes/sport)。
有谁知道我如何让它正常工作并替换所有匹配项和模式,或者是否有任何其他 clever/smarter 方法来获得我需要的东西?
PS:这个 angular 项目是一个 Ionic 平台(移动应用程序),我正在从与桌面版本共享的数据库中检索数据(文本 + 链接)(php),通常会插入这些相对路径。因此我需要完成它们,否则我的移动应用程序中的许多链接将被破坏。
感谢您的帮助!
您可以尝试使用正则表达式。类似的东西:
var str = 'Here you can find the best <a href="/shoes/sport">sport shoes</a>, <a href="/shoes/classic">classic shoes</a> and <a href="/socks/sale">socks</a>. If not, you can <a href="http://www.google.com">google</a> it.';
var res = str.replace(/href=\"\//g, "href=\"http://www.example.com/");
正则表达式中的'g'是全局替换。
我是 angular 的新手,我正在努力替换文本中的相对 href 路径。
为了举例,我有以下 controller/scope/html/filter:
$scope.text = 'Here you can find the best <a href="/shoes/sport">sport shoes</a>, <a href="/shoes/classic">classic shoes</a> and <a href="/socks/sale">socks</a>. If not, you can <a href="http://www.google.com">google</a> it.';
<p ng-bind-html="text | parseUrlFilter"></p>
我试图创建一个过滤器来搜索所有相关 urls 并替换为完整路径 url(例如:http://www.example.com/shoes/sport, http://www.example.com/shoes/classic, http://www.example.com/socks/sale):
app.filter('parseUrlFilter', function() {
var urlPattern1 = 'href="/shoes/';
var urlPattern2 = 'href="/socks/';
return function(text) {
angular.forEach(text.match(urlPattern1), function(url) {
url = url.replace('href="','');
text = text.replace(url, 'http://www.example.com' + url);
});
angular.forEach(text.match(urlPattern2), function(url) {
url = url.replace('href="','');
text = text.replace(url, 'http://www.example.com' + url);
});
return text;
};
})
但是,我的过滤器仅替换每个模式的第一个案例 (http://www.example.com/shoes/sport)。
有谁知道我如何让它正常工作并替换所有匹配项和模式,或者是否有任何其他 clever/smarter 方法来获得我需要的东西?
PS:这个 angular 项目是一个 Ionic 平台(移动应用程序),我正在从与桌面版本共享的数据库中检索数据(文本 + 链接)(php),通常会插入这些相对路径。因此我需要完成它们,否则我的移动应用程序中的许多链接将被破坏。
感谢您的帮助!
您可以尝试使用正则表达式。类似的东西:
var str = 'Here you can find the best <a href="/shoes/sport">sport shoes</a>, <a href="/shoes/classic">classic shoes</a> and <a href="/socks/sale">socks</a>. If not, you can <a href="http://www.google.com">google</a> it.';
var res = str.replace(/href=\"\//g, "href=\"http://www.example.com/");
正则表达式中的'g'是全局替换。