AngularJS function scope,how to call a function

AngularJS function scope,how to call a function
0

#1

I have a doubt about how works AngularJS scope, because I wanna to create a function on the controller, but I wanna to call when user clicks.
For example I have a ng-click=“click()”, but I need to get a value from another function in the controller and when clicks decrement the value.

  app.controller('Controller', ['$http','$scope', ' YoutubeService'  function ($http, $scope,  YoutubeService) {
  
  YoutubeService.getPlaylistItems(id, $scope.nextPageToken)
  .then(function (response) {
          
     $scope.youtubePlaylist = response.items;
     **$scope.variable = 50;**// this value is from request, number is just an example, I need to pass this value to a click function
         });

 $scope.loadMore = function( itemId, nextPage) { 
          anotherfunction( itemId, nextPage);   
        **$scope.variable =  $scope.variable - 5;** // here will decrement every time it clicks
       });

#2

Why not just initialize the variable outside your YoutubeService, inside your ‘Controller’ controller.

$scope.variable = 0;

then update it’s value inside your YoutubeService .then function().

loadMore will be aware of the new value of $scope.variable.


Now, there’s another technique if you want a “global” variable whose value is persistent and you can access from different controllers. But that doesn’t seem to be what you want, right?


#3

Something is wrong, it’s working the way I spected. I will explain more, I am trying to make results pagination of youtube videos, so the first time loads, show 5 videos and in the API have a property totalResults that bring 40 results.

So that variable should start with the value of totalResults, and when I load more I thought that subtracting 5 and when variable where 0, disable the button, because it’s repeating results.

I try to create the variable inside request to get the value 40, but I can’t reduce 5 every time the button is clicked, to disable the button.

Complete code:
CodePen