9.3 Adding $scope.plusOne


#1

https://www.codecademy.com/en/courses/learn-angularjs/lessons/your-first-app/exercises/your-first-app-ng-click-i

added a function $scope.plusOne.. but giving error

$scope.plusOne = function(index) {
$scope.products[index].likes += 1;
};


#2

What error message are you seeing?

Have you added a likes property to each product object?

        {
            name: 'The Book of Trees',
            price: 19,
            pubdate: new Date('2014', '03', '08'),
            cover: 'img/the-book-of-trees.jpg',
            likes: 0
        }

#3

Hello,
I have the same issue. How to solve.? I cant see any error message displayed. I have added likes. Below is my code.

app.controller('MainController', ['$scope', function($scope) { 
  $scope.title = 'Hello Poombavai';
  $scope.promo = 'awesome';
  $scope.products = 
  [ 
  { 
    name: 'The Book of Trees', 
    price: 19, 
    pubdate: new Date('2014', '03', '08'), 
    cover: 'img/the-book-of-trees.jpg',
    likes: 0,
    plusOne: function(index) {
      $scope.products[index].likes += 1;
     }
  }, 
  { 
    name: 'Program or be Programmed', 
    price: 8, 
    pubdate: new Date('2013', '08', '01'), 
    cover: 'img/program-or-be-programmed.jpg',
    likes: 0,
    plusOne: function(index) {
      $scope.products[index].likes += 1;
    }
  },
  { 
    name: 'Wings of fire', 
    price: 9, 
    pubdate: new Date('2010', '04', '07'), 
    cover: 'img/program-or-be-programmed.jpg',
    likes: 0,
    plusOne: function(index) {
      $scope.products[index].likes += 1;
    }
  },
  { 
    name: 'My religion is Kindess', 
    price: 10, 
    pubdate: new Date('1994', '02', '28'), 
    cover: 'img/program-or-be-programmed.jpg',
    likes: 0,
    plusOne: function(index) {
      $scope.products[index].likes += 1;
    }
  }];

Where is the bug? Please help.

#4

This should be a controller variable outside of the products array.

  $scope.plusOne = function(index) {
    $scope.products[index].likes += 1;
  };

#5

Understood. Thank you so much @mtf.


#6

Still, it does not work.I have added same code i tried many with it but not work.


#7

app.controller('MainController', ['$scope', function($scope) {
$scope.title = 'devashish ';
$scope.promo = 'devaishoiasu';
$scope.products = [{
likes:'0',
name: 'The Book of Trees',
price: 19,
pubdate: new Date('2014', '03', '08'),
cover: 'img/the-book-of-trees.jpg'
},
{
likes:'0',
name: 'Program or be Programmed',
name: 'Program or be Programmed',
price: 8,
pubdate: new Date('2013', '08', '01'),
cover: 'img/program-or-be-programmed.jpg'
},
{
likes:'0',
name: 'book_1',
price: 8,
pubdate: new Date('2013', '08', '01'),
cover: 'img/program-or-be-programmed.jpg'
},
{
likes:0,
name: 'book_2',
price: 8,
pubdate: new Date('2013', '08', '01'),
cover: 'img/program-or-be-programmed.jpg'

}
];
$scope.plusOne =
function(index) {
$scope.products[index].likes += 1;
};

}]);


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.