我正在一个有角度的应用程序中创建一个URL列表,大多数URL都导致 https://
example.io
...但是,只有少数具有诸如< strong> https://
example.io
。我不想在每个对象中都指定URL……我只是想验证URL,然后根据有效内容使用http://
或https://
打开网站。
我正在使用ng-click,并将其绑定到带有嵌套了w /图片的网址打开的窗口。
Index.html
<head>
<script src="js/shared/angular.min.js"></script>
<link rel="stylesheet" href="css/row.css">
</head>
<body ng-app="MainApp">
<div ng-controller="Main" ng-cloak>
<div id="RowDiv">
<button style="visibility: hidden; width: 25px;"></button>
<a ng-repeat="r1 in r1s" ng-click="openurl(r1.url.$valid==true?r1.url:'https://' + r1.name + '.io')" ng-cloak>
<img ng-src="img/{{r1.name}}.png" height="40px">
</a>
<button style="visibility: hidden; width: 25px;"></button>
</div>
</div>
<button id="LftBtn" style="z-index: 1000;" >
<i id="L-Arrow"></i>
</button>
<button id="RgtBtn" style="z-index: 1000;" >
<i id="R-Arrow"></i>
</button>
<script src="js/protractor.js"></script>
<script src="js/row.js"></script>
</body>
protractor.js
var app = angular.module("MainApp", []);
// - - app controllers - - \\
app.controller('Main', ['$scope', function($scope) {
$scope.r1s = [
{name: 'Surviv', url: 'https://google.com'},
//Just a test, urls will be changed later
{name: 'Zombsroyale'},
{name: 'Bruh'},
{name: 'Krunker'},
{name: 'Shellshock'},
{name: 'Diep'},
{name: 'Tanked'},
{name: 'Gats'},
{name: 'Warbot'},
{name: 'Counterstroke'}
];
$scope.openurl = function(url){
if(window.parent == window.top) {
//If outside
window.open(url, "_blank"});
} else {
//If inside
window.open(url, '_parent');
}
};
}]);
这里的主要内容是url
验证,
和 ng-click ="openurl(r1.url.$valid == true ? 'http://' + r1.name '.io' : 'https://' + r1.name + '.io')"
我不知道为什么它不起作用,它将打开弹出窗口,但是它将完全忽略三进制和$ valid参数...
我尝试过交换
-.$valid==true
w /
-.$valid==false
但我得到相同的结果!为什么这不起作用?
关于plunkr,jsfiddle或codepen等的工作示例将很棒。
答案 0 :(得分:0)
经过长时间的搜索... 我偶然发现了This网站... 实施后,代码将如下所示:
Index.html更改
ng-click="openurl(r1.url.$valid==true?r1.url:'https://' + r1.name + '.io')"
到
ng-click="is_url(r1.name+'.io')"
protractor.js更改/添加
{name: 'Surviv', url: 'https://google.com'},
到
{name: 'Surviv'},
在主控制器内:
添加:
$scope.is_url = function(str) {
regexp = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;
if (regexp.test('https://' + str))
{
chrome.windows.create({url: 'https://' + str, type: "popup"});
}
else
{
chrome.windows.create({url: 'http://' + str, type: "popup"});
}
};
在上一个示例/用法中,我使用了window.open
此后,我将其更改为chrome.windows.create
与window.open
一起使用-
更改{url: 'http://' + str, type: "popup"}
或{url: 'https://' + str, type: "popup"}
发送至:'http://' + str, "_blank"
或'https://' + str, "_blank"
。
这花了太多时间进行谷歌搜索...希望所有遇到此问题的人现在都能找到一种更简单的使用方法...