2016-08-03 46 views
0

我想比较这两个函数的variable值。不幸的是,我试图使variablesglobal但我不断得到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 
    } 
}); 

我想检查是否callmecrazierVal !== callmecrazyVal。这是我上面完整的controller代码。

if(callmecrazierVal !== callmecrazyVal){ 
    //trigger some other function 
} 
+2

'var'使它们本地,如果你想他们全球删除它。你也可以使用'return'而不是控制台日志,然后比较函数调用。 –

+0

这些回应来自哪里? – Bergi

+0

@Bergi我将编辑代码以查看它们来自哪里! –

回答

1

如果由函数返回的值是不是基本类型,你应该:

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

编辑 我假设你想以10秒的间隔点击api并检查如果有任何改变,对吧? (否则,请澄清你想达到的目标)。如果是这样,你需要链接异步调用,然后比较如下:

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

}); 

}); 
+0

angular.js:13920 ReferenceError:callmecrazierVal未定义 –

+0

请参阅编辑。您应该比较两个函数的返回值。 –

+0

angular.js:13920 TypeError:无法读取未定义的属性'国家' 在callmecrazier –

2

你应该从你的函数中返回一些东西。返回的值将被比较。

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

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

然后,像这样对它们进行比较:

if(callmecrazierVal() !== callmecrazyVal()){ 
    //trigger some other function 
} 
+0

它说callmecrazierVal没有定义。 –

+0

1.确保您从同一范围调用此函数。 2.调用此函数时不要忘记括号'()'。 –

+0

我添加了更多的代码,所以你看到哪里得到的答复。 –

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........... 
} 
+0

ReferenceError:response1未定义。我将编辑代码以添加获取响应值的方式。 –

+0

传递函数中的值。我认为回应1是你的元素。 –