Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript URL重定向问题_Javascript_Angularjs_Url_Redirect - Fatal编程技术网

Javascript URL重定向问题

Javascript URL重定向问题,javascript,angularjs,url,redirect,Javascript,Angularjs,Url,Redirect,URL重定向不起作用,即 我正在使用过滤器替换与我的锚标签匹配的字符串 控制器 var app = angular.module('app', ['ngSanitize']); app.filter('parseUrlFilter', function ($sce) { var urlPattern = /(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi; return function (text, t

URL重定向不起作用,即

我正在使用过滤器替换与我的锚标签匹配的字符串

控制器

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

app.filter('parseUrlFilter', function ($sce) {
    var urlPattern = /(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi;
    return function (text, target, otherProp) {
           text = text.replace(urlPattern, '<a target="' + target + '" href="$&">$&</a>');
           return $sce.trustAsHtml(text);
    };
});

function Ctrl($scope) {
    $scope.text = 'Example text http://example.com http://example.com http://google.com google.com';
}
var-app=angular.module('app',['ngSanitize']);
app.filter('parseUrlFilter',函数($sce){
var urlPattern=/(^ |\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)(\/\s*))/gi;
返回函数(文本、目标、其他属性){
text=text.replace(urlPattern“”);
返回$sce.trustAsHtml(文本);
};
});
函数Ctrl($scope){
$scope.text='示例文本http://example.com http://example.com http://google.com 谷歌(google.com),;
}
HTML


我的问题是,当url具有http或https时,它会将我们正确地重定向到相应的页面,即如果它工作正常。但如果
http或https丢失,则无法正常工作,即如果google.com它不会重定向到google.com,而是重定向到localhost/folder/google.com

这是因为如果url是绝对的,则href需要协议,否则它被认为是相对于当前url的。你能告诉我解决方案吗?这是因为如果url是绝对的,则href需要协议,否则它被认为是相对于当前url的。你能告诉我解决方案吗?
<div ng-controller="Ctrl">
     <p ng-bind-html="text | parseUrlFilter:'_blank'"></p>
</div>