Checking for URL.$valid (if then) inside inside AngularJS ng-click. How?


I am making a URL list inside an angular App, Most of the URLs all lead to… However a select few have URLs such as I don’t want to specify the URL inside every object… I just want to validate the URL then either open the website with http:// or https:// depending on what’s valid.

I’m using ng-click and binding it to a window open with the url along w/ image nested.


    <script src="js/shared/angular.min.js"></script>
    <link rel="stylesheet" href="css/row.css">

    <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://' + + '.io')"  ng-cloak>
            <img ng-src="img/{{}}.png" height="40px">
            <button style="visibility: hidden; width: 25px;"></button>

    <button id="LftBtn" style="z-index: 1000;" >
        <i id="L-Arrow"></i>

    <button id="RgtBtn" style="z-index: 1000;" >
        <i id="R-Arrow"></i>

    <script src="js/protractor.js"></script>
    <script src="js/row.js"></script>


    var app = angular.module("MainApp", []);

    // - - app controllers - - \\

    app.controller('Main', ['$scope', function($scope) {
      $scope.r1s = [
        {name: 'Surviv', url: ''},
        //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 == {
          //If outside
, "_blank"});
        } else {
          //If inside
, '_parent');

The main thing here is the url validation,
and the ng-click="openurl(r1.url.$valid == true ? 'http://' + '.io' : 'https://' + + '.io')"

I am clueless as to why this isn’t working, it will open the popup, but it will ignore the ternary & $valid parameters completely…
I’ve tried swapping the
but I get the same result! Why’s this not working?

A working example on plunkr, jsfiddle, or codepen, etc would be great.


After long hours of searching…
I stumbled upon This Website…
After implementation the code would look like this:

Index.html changes

ng-click="openurl(r1.url.$valid==true?r1.url:'https://' + + '.io')"

protractor.js changes/ additions

{name: 'Surviv', url: ''},
{name: 'Surviv'},

inside the main controller:

$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))
  {url: 'https://' + str, type: "popup"});
  {url: 'http://' + str, type: "popup"});

I used in the previous example/use
I have since changed it to
to use this with
change {url: 'http://' + str, type: "popup"} Or {url: 'https://' + str, type: "popup"}

to: 'http://' + str, "_blank" Or 'https://' + str, "_blank".

This took way too many hours of googling… Hopefully everyone else who ever runs in to this issue will now find an easier method of use…

Answered By – 255.tar.xz

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.