During a lesson on AngularJS from Code Academy, the following code was presented. The lesson included a rating system where users could choose to 'like' or 'dislike' an item. Clicking the 'like' button would increase the vote count, while clicking 'dislike' would decrease it. However, the code does not include a way to limit a user's voting to only once. Is there a method in AngularJS to restrict user voting to just one instance?
Sample HTML code:
<div class="rating">
<p class="likes" ng-click="plusOne($index)">+{{product.likes}}</p>
<p class="dislikes" ng-click="minusOne($index)">-{{product.dislikes}}</p>
</div>
AngularJS code:
app.controller('MainController', ['$scope',
function($scope) {
$scope.title = 'Top Sellers in Books in this Great City';
$scope.promo = 'Our Christmas Promo is on the way!';
$scope.products = [{
name: 'The Book of Trees',
price: 19,
pubdate: new Date('2014', '03', '08'),
cover: 'img/the-book-of-trees.jpg',
likes: 0,
dislikes: 0
}, {
name: 'Program or be Programmed',
price: 8,
pubdate: new Date('2013', '08', '01'),
cover: 'img/program-or-be-programmed.jpg',
likes: 0,
dislikes: 0
}, {
name: 'Book1',
price: 8,
pubdate: new Date('2015', '04', '02'),
cover: 'img/..',
likes: 0,
dislikes: 0
}, {
name: 'Book2',
price: 8,
pubdate: new Date('2015', '09', '09'),
cover: 'img/..',
likes: 0,
dislikes: 0
}];
$scope.plusOne = function(index) {
$scope.products[index].likes += 1;
};
$scope.minusOne = function(index) {
$scope.products[index].dislikes += 1;
};
}
]);