0

I would like to compare the variable values of this two functions. Unfortunately I have tried to make the variables global but I keep getting undefined.

'use strict';

/**
 * @ngdoc function
 * @name dashyAppApp.controller:Dashy3Ctrl
 * @description
 * # Dashy3Ctrl
 * Controller of the dashyAppApp
 */

angular.module('dashyAppApp')
  .controller('Dashy3Ctrl', function ($rootScope, $scope, employees, $interval) {
   var _this = this;

  $scope.getData = function() { // getDATA function
    employees.getEmployees().then(function(response){
        _this.items = response.data;
        $scope.items = _this.items;

       callmecrazy(response.data);
    });

  }// End getDATA function
  $scope.getData();

  $scope.getDataB = function() {
  employees.getEmployees().then(function(response){
    _this.items = response.data;
     callmecrazier(response.data);
  });
 }
 $interval(function(){
 $scope.getDataB();
 }, 10000);

 function callmecrazier(response1){
   callmecrazierVal = response1.countries;
    return callmecrazierVal;
 }

 function callmecrazy(response){
  callmecrazyVal = response.countries;
  return callmecrazyVal;
 }

 if(!angular.equals(callmecrazierVal(), callmecrazyVal())) {
    //trigger some other function
  }
});

I want to check if callmecrazierVal !== callmecrazyVal. That is my complete controller code above.

if(callmecrazierVal !== callmecrazyVal){
  //trigger some other function
}
Aakash
  • 1,455
  • 13
  • 16
LearnToday
  • 2,762
  • 9
  • 38
  • 67

3 Answers3

2

You should return something from your functions. The returned value will them be compared.

function callmecrazier(response1){
   var callmecrazierVal = response1.countries;
    return callmecrazierVal;
 }

 function callmecrazy(response){
  var callmecrazyVal = response.countries;
  return callmecrazyVal;
 }

Then compare them like this:

if(callmecrazierVal() !== callmecrazyVal()){
  //trigger some other function
}
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • it says callmecrazierVal is not defined. – LearnToday Aug 03 '16 at 04:01
  • 1. Make sure that you're calling this function from the same scope. 2. Do not forget parenthesis `()` while calling this function. – Raman Sahasi Aug 03 '16 at 04:10
  • I added more code so u see where am getting the response . – LearnToday Aug 03 '16 at 04:12
  • where is the `if` condition in your code? If you're using `if(callmecrazierVal !== callmecrazyVal){` then you have forgotten parenthesis. Use `if(callmecrazierVal() !== callmecrazyVal() ){` instead – Raman Sahasi Aug 03 '16 at 04:16
  • you are assuming values returned are primitives. – Chirag Kothari Aug 03 '16 at 04:17
  • @ObasiObenyOj **(1)** you've written your `if` condition directly in your controller. How will it get executed? You should write it in some function block and then you should call that function. **(2)** both of your functions i.e., `callmecrazier` and `callmecrazy` expects some parameter. Where are you passing those parameters when you're calling those functions from `if` condition? – Raman Sahasi Aug 03 '16 at 05:07
1

If the values returned by the functions are not primitives you should:

if(!angular.equals(callmecrazier(), callmecrazy()) {
   //trigger some other function
}

EDIT I am assuming you want to hit api at interval of 10secs and check if there are any changes, right? (else, please clarify what you are trying to achieve). If so you need to chain the async calls and then compare like below:

'use strict';

 /**
  * @ngdoc function
  * @name dashyAppApp.controller:Dashy3Ctrl
  * @description
  * # Dashy3Ctrl
  * Controller of the dashyAppApp
 */

angular.module('dashyAppApp')
 .controller('Dashy3Ctrl', function($rootScope, $scope, employees, $interval)            {
 var _this = this;

 $scope.getData = function() { // getDATA function
     return employees.getEmployees().then(function(response) {
       return response.data; 
     });

   } // End getDATA function


$scope.getData()
.then(function(data1){
   $scope.items = data1; 
   $interval(function() {
     $scope.getData()
     .then(function(data2){
        if (!angular.equals($scope.items, data1) {
          //trigger some other function
        }
     });
   }, 10000);

});

 });
Chirag Kothari
  • 1,390
  • 9
  • 19
0
function callmecrazier(response1){
   var callmecrazierVal = response1.countries;
   return callmecrazierVal;
}

function callmecrazy(response){
   var callmecrazyVal = response.countries;
   return callmecrazyVal;
}

if(callmecrazier(response1) !== callmecrazy(response)){
   //Your code...........
}
Jaydeep Mor
  • 1,690
  • 3
  • 21
  • 39