简体   繁体   中英

How can I compare the values between this two functions?

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
}

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
}

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);

});

 });
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...........
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM