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