Skip to content

Instantly share code, notes, and snippets.

@daman2408
Created March 21, 2016 21:53
Show Gist options
  • Save daman2408/ff932ae381b65305525a to your computer and use it in GitHub Desktop.
Save daman2408/ff932ae381b65305525a to your computer and use it in GitHub Desktop.
Suggestion Box - Routing issue
var app = angular.module('SuggestionBox', ['ngRoute'])
app.config(function($routeProvider){
$routeProvider
.when('/', {
controller:'HomeController',
templateUrl:'views/home.html',
})
.otherwise({
redirectTo:'/'
});
});
<h1>{{ helloWorld }}</h1>
<div ng-repeat="post in posts | orderBy: '-upvotes'">
<p>{{post. title}}</p>
<span class="glyphicon glyphicon-plus-sign" ng-click="upVote(post)">{{post.upvotes}}</span>
<div>{{post.comments}}</div>
</div>
<form ng-submit="addSuggestion()" style="margin-top: 50px;">
<h3>Submit Your Suggestion</h3>
<div class="form-group">
<input type='text' class='form-control' placeholder="Great Ideas Here" ng-model="title"></input>
</div>
<button type="submit" class="btn btn-primary">Suggest</button>
</form>
app.controller('HomeController', ['$scope','suggestions', function($scope, suggestions){
$scope.helloWorld = 'Hello, AngularJS!';
$scope.posts = suggestions.posts;
$scope.addSuggestion = function(){
//if input is empty
if(!$scope.title || $scope.title == ''){
return;
}
//push suggestions into box
$scope.posts.push({
title: $scope.title,
upvotes:0,
});
//after submit, clear input
$scope.title='';
};
$scope.upVote = function(post){
post.upvotes += 1;
}
}]);
<!DOCTYPE html>
<html>
<head>
<title>Suggestion Box</title>
<script type="text/javascript" src="../angular.min.js"></script>
<link src="https://code.angularjs.org/1.2.28/angular-route.min.js" type="text/javascript">
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/
css/bootstrap.min.css" rel="stylesheet">
</head>
<body ng-app="SuggestionBox">
<div ng-view></div>
<!-- Modules -->
<script type="text/javascript" src="JS/app.js"></script>
<!-- Controllers -->
<script type="text/javascript" src="JS/Controllers/HomeController.js"></script>
<!-- Services -->
<script type="text/javascript" src ="JS/Services/suggestions.js"></script>
</body>
</html>
app.factory('suggestions', [function(){
var demoSuggestions = {
posts:[
{
title: ' Free pizza at club meetings',
upvotes: 15,
comments: [],
},
{
title: 'End all club emails with Laffy Taffy jokes',
upvotes: 9,
comments: [],
},
{
title: 'Retrofit water fountain with Gatorade',
upvotes: 7,
comments: [],
},
{
title: 'Sing Bon Jovi\'s "Living on a Prayer" halfway through meetings',
upvotes: 3,
comments: [],
},
{
title: 'Will Self-Driving Cars Ruin our Transportation Industry?',
upvotes: 35,
comments: [],
},
]};
return demoSuggestions;
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment