My first suggestion would be to not use the callbacks. Using Angulars $q promise library would make things a lot simpler. So, your “FriendService” would return a promise. Then, your controller would use the .then method to put the results on the scope after the promise completes.
If that’s not familiar territory, I’d suggest watching the Egghead.io videos. They’re really helpful.
Other than that, what is the exact problem right now? When you type in the search field and click “Search”:
Do you get results from “FriendService”?
Does the $scope.searchuserCallBack get called?
Does your list not update with the results?
If item 1 & 2 are working and item 3 is not working, I’m guessing it’s because you aren’t using “dot notation”.
If so, try this:
<ion-content has-header="true" has-tabs="true">
<h3>Search results:</h3>
<ion-list>
<ion-item ng-repeat="user in data.foundUsers" type="item"> <!-- NOT THE DOT NOTATION! -->
<h3>{{user.username}}</h3>
</ion-item>
</ion-list>
</ion-content>
.controller('FriendCtrl', function($scope, $state, $stateParams, FriendService) {
$scope.model = {username:"", search:""};
$scope.data = {
"foundUsers" : [] // INITIALLY SET FOUND USERS TO AN EMPTY ARRAY
};
$scope.$watch("foundUsers", function(newValue, oldValue){
alert("test"); // why alert only after the second click to Search button?
});
$scope.searchUserCallBack = function(foundUsers) {
alert(JSON.stringify(foundUsers));
$scope.data.foundUsers=foundUsers.users; // NOW SET FOUNDUSERS TO THE NEW DATA. NOTE THE DOT NOTATION.
}
$scope.searchUser = function() {
var output = FriendService.searchUser($scope.model.search, $state, $scope.searchUserCallBack); // an async call that finally sets the foundUsers variable using the callback.
}
I don’t see explicitly $q promise library usage in your code.
FYI, I use the library directly: using
var deferred = $q.defer();
…
…
//if OK -
deferred.resolve(
//if NOK -
deferred.reject( … )
…
return deferred.promise;
also I am using … var client = new XMLHttpRequest(); instead of $http.jsonp (i think this is sync??).
you can see it this file how to use the promises lib:
For example the following function:
findById: function(employeeId) {
var deferred = $q.defer();
var employee = employees[employeeId - 1];
deferred.resolve(employee);
return deferred.promise;
}
Actually the promise library is not required in this function, yet it was made like this so you can easily extend to use REST call instead of the in memory array access.
try to replace this line:
var employee = employees[employeeId - 1];