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

1
  • Like this ? Commented Jan 30, 2015 at 13:50

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's another good way of doing it similar to what I originally thought.
Thanks, I forgot. This was supposed to be angular.. I'll change the code.
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

Comments

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.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.