2016-08-03 5 views
0

この2つの機能の値をvariableと比較したいと思います。残念ながら私はvariablesglobalを作りましたが、私はundefinedを得続けます。この2つの関数の値を比較するにはどうすればよいですか?

'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 
} 

を私が想定していEDIT 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

編集を参照してください。 2つの関数の戻り値を比較する必要があります。 –

+0

angular.js:13920 TypeError:未定義のプロパティ 'countries'を読み取ることができません 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

関数の値を渡します。私はresponse1があなたの要素だと思います。 –

関連する問題