Service
angular.module('tested-user')
.service('tests', function($http, $q){
this.getTests = $q.when($http.get('http://localhost:3000/tests.json'));});
Controller
tests.getTests.then(function(data){
noOfTests = data.data.tests.length;
$scope.testsForSearch = data.data.tests;
while($scope.testsForSearch.length){
if($scope.testsForSearch.length > 1 )
{
$scope.tests.push(testsForSearch.splice(0,2));
}
else{
$scope.tests.push(testsForSearch.splice(0,1));
}
}
// data is empty here also
console.log(data);
}
Function in the Controller that uses the scope variable
$scope.search = function(){
console.log($scope.testsForSearch.tests);
$scope.testsForSearch.tests.forEach(function(test){
if(test.name.search($scope.searchInput)){
$scope.tests.push(test);
}
});
}
The scope.search function will be triggered by a click event. It appears that the 'search' function is unable to access data stored in 'scope.testsForSearch', which is available in the views and where the 'tests.getTests' function is executed.