There is a factory

appModule.factory('search', ['$http', function($http) { return { getObj: function(url, termParam, entity, array, checkParam) { $http.jsonp(url, { params: { "callback": "JSON_CALLBACK", "term": termParam, "limit": 25, "entity": entity } }).success(function(data, status, headers, config) { array = data.results; if (array.length == 0) { checkParam = false; } else { checkParam = true; } }).error(function(data, status, headers, config) { console.log("error" + data); }); } } }]); 

and factory call

 itunesSearch.getObj("http://itunes.apple.com/search", $scope.searchParam, "musicVideo", $scope.videoArray, $scope.videoCheck); 

I can not understand why the parameters:
$ scope.videoArray
$ scope.videoCheck
return undefind

    1 answer 1

    In javascript, all parameters are passed by reference, so you cannot assign a value to a parameter and expect that the variable will change from the outside.

    This is because the parameter simply starts to reference the new object inside the function.

    To solve, you must either return the necessary objects:

     }).success(function(data, status, headers, config) { return {array:data.results, checkParam:data.results.length != 0}; }) 

    And further in the code:

     itunesSearch.getObj("http://itunes.apple.com/search", $scope.searchParam, "musicVideo").then(data=>{ $scope.videoArray=data.array; $scope.videoCheck=data.checkParam; }); 

    Either pass an object to a function, and change its field values. But since the function is asynchronous, the values ​​will change only after receiving the answer.


    In order to find out how to get values ​​from asynchronous functions, you can refer to the following question:

    How to return a value from an event or from a callback function?