-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
100 lines (89 loc) · 2.59 KB
/
main.js
File metadata and controls
100 lines (89 loc) · 2.59 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var app = angular.module('FirstApplication', []);
//angular.module('FirstApplication')
app.service('CommentService',function(){
var coments = [
{
productId:1,
author: "author1",
message: "hello1"
},
{
productId:2,
author: "author2",
message: "hello2"
}
]
this.getByProductId = function(id){
var res = [];
for (var i = 0; i < coments.length; i++) {
var comment = coments[i];
if(comment.productId ==id)
res.push(comment)
}
return res;
}
this.addComment = function(comment){
coments.push(comment)
return this.getByProductId(comment.productId)
}
})
app.service('ProductService',function(){
var product = {
id: 1,
description: "bla bla",
img: "http://thumbs.dreamstime.com/z/christmas-basket-goods-26388714.jpg",
}
var products = [
{
id: 1,
description: "bla bla",
img: "http://thumbs.dreamstime.com/z/christmas-basket-goods-26388714.jpg",
},
{
id: 2,
description: "bla bla",
img: "http://thumbs.dreamstime.com/z/christmas-basket-goods-26388714.jpg",
},
{
id: 3,
description: "bla bla",
img: "http://thumbs.dreamstime.com/z/christmas-basket-goods-26388714.jpg",
},
]
this.getProductById = function(id){
for (var i = 0; i < products.length; i++) {
var obj = products[i];
if(obj.id == id) return obj;
}
}
this.getAll = function(){
return products;
}
})
app.directive('product',function(){
return {
scope:{
product:"=data",
},
replace:true,
restrict:"E",
controller:'ProductController',
templateUrl:'./product.template.html'
}
})
app.controller('ProductsListController',function($scope,ProductService){
$scope.products = ProductService.getAll();
})
app.controller('ProductController', function ($rootScope,$scope,CommentService,
ProductService) {
$rootScope.$broadcast('hrllo',1,2,3)
$rootScope.$on('hrllo',function(e,data){
console.log(data)
})
$scope.product.comments = CommentService.getByProductId($scope.product.id);
$scope.sendComment = function(){
$scope.newComment.productId = $scope.product.id;
$scope.product.comments = CommentService.addComment($scope.newComment)
$scope.newComment = {}
}
})