1

I'm new to Angular.js and trying to build a simple news app where you post some text and that text is linked to a url.

This is my HTML:

<html>
<head>
    <title>Angular News App</title>

    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
    <script src="app2.js"></script>

</head>
<body ng-app="News" ng-controller="MainCtrl">

    <div ng-repeat="post in posts">
        <a href="{{post.link}}">
            {{post.title}}
        </a>
    </div>
    <form ng-submit="addPost()">
        <input type="text" placeholder="Title" ng-model="title"></input>
        <br>
        <input type="text" placeholder="Link" ng-model="link"></input>
        <br>
        <button type="submit">Post</button>
    </form>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</body>

And here is my app2.js:

angular.module('News', [])
.controller('MainCtrl', [
    '$scope',
    function($scope){
        $scope.posts = [
            {title: 'Hotmail.com', link: 'http://www.hotmail.com'},
            {title: 'Facebook.com', link: 'http://facebook.com'}
        ];
        $scope.addPost = function(){
            if(!$scope.title || $scope.title === ''){ return;}
            if(!$scope.link || $scope.link === ''){ return;}
            // if statement to be added here?           

            $scope.posts.push({
                title: $scope.title, 
                link: $scope.link
            });
            $scope.title= '';
            $scope.link= '';
        };
    }] );

This all works.

Now I want to check if the link provided starts with "http://" and if it doesn't then concatenate that to the start of $scope.link. Can anyone tell me how to do this?

I feel it should be possible using something like:

if(!$scope.link.startsWith("http://)){
    $scope.link = "http://" + $scope.link;}

and inserting this after the other if statements in app2.js.

Thanks in advance it is much appreciated!

Tom

TomCee
  • 11
  • 3
  • [Like this ?](http://stackoverflow.com/questions/646628/how-to-check-if-a-string-startswith-another-string) – merours Jan 30 '15 at 13:50

4 Answers4

1

You can use this:

if(!$scope.posts.link.indexOf("http://")){
   //Logic when url starts with HTTP protocol
}

This check if the link you have provided have the string "http://" at the beginning.

Carlo Gonzales
  • 365
  • 2
  • 9
0

You can use $location like this:

var absolute_url = $location.absUrl();
var first_part = absolute_url.substring(0, 7);

if(first_part === 'http://'){
    # do something here
}

Do not forget to inject $location to the controller

Jahongir Rahmonov
  • 13,083
  • 10
  • 47
  • 91
0

After spending all that time writing the question. I have just managed to work out the answer!:

if($scope.link.substring(0, 7) != "http://"){
    $scope.link = "http://" + $scope.link;}

I thought I'd leave this here incase anyone else wanted to do something similar.

TomCee
  • 11
  • 3
0

what about https?

var url = 'https://www.google.es';
//var url = 'http://www.google.es';
//var url = 'www.google.es';

if(!url.match(/^https?:\/\//))
{
        url = 'http://'+url;
}
console.log(url);
ZiTAL
  • 3,466
  • 8
  • 35
  • 50