-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (63 loc) · 2.21 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var app = angular.module('MEANReddit', ['ui.router']);
//Creating config block and configure a home state. Use otherwise() to
//redirect to unspecified routes.
//Here we set up our home route. You'll notice that the state is given
// a name ('home'), URL ('/home'), and template URL ('/home.html').
//We've also told Angular that our new state should be controlled by
//MainCtrl. Finally, using the otherwise() method we've specified what
//should happen if the app receives a URL that is not defined. All
//that's left to do is define the home.html template. Instead of
//creating a new file, we are going to move most of our HTML into an inline template.
// app.config([
// '$stateProvider',
// '$urlRouterProvider',
// function($stateProvider, $urlRouterProvider){
// $stateProvider
// .state('home', {
// url: '/home',
// templateUrl: '/home.html',
// controller: 'MainCtrl'
// });
// $urlRouterProvider.otherwise('home');
// }]);
//Creating a factory for posts:
// Creating a new object that has an array property called "posts"
//We then return "o" so that object becomes exposed to any other
//Angular module that cares to inject it. We could've exported array
//directly but by exporting an object containing the posts array
//we can add new objects and methbods to our services in the future.
app.factory('posts', [function(){
var o = {
posts: []
};
return o;
}])
app.controller('MainCtrl', [
'$scope',
'posts', //Inject posts service into the controller
function($scope, posts){ //Inject posts service into the controller
// $scope.posts = [
// {title: 'post 1', upvotes: 5},
// {title: 'post 2', upvotes: 3},
// {title: 'post 3', upvotes: 22},
// {title: 'post 4', upvotes: 8},
// {title: 'post 5', upvotes: 12}
// ];
$scope.addPost = function(){
if(!$scope.title || $scope.title === ''){
return;
};
$scope.posts.push({
title: $scope.title,
link: $scope.link,
upvotes: 1
});
$scope.title = '';
$scope.link = '';
};
$scope.incrementUpvotes = function(post){
post.upvotes +=1;
};
//experiment
$scope.posts = posts.posts; //Binding $scope.posts variable in controller to posts array in our service
}]);