a newbie question.

making a server call via a service and want to know where to update $scope. 
In controller or in service?
Boths works. 



<input type="text" name="queryFor" ng-keyup="query()" ng-model="queryFor"/>
<div ng-repeat="result in results">{{ result.title }}</div>



1. In controller.
myApp..controller('Controller', function($scope, dataService) {
    $scope.results = [];
    $scope.queryFor = "";
    $scope.query = function() {
        dataService.query($scope.queryFor).success(function(data, status, 
headers, config) {
                $scope.results = data.results;
            });
    };
})



myApp.factory('dataService', ['$http', function($http) {
    var dataService = {
        query: function(queryFor) {
            $http.get('http://www.example.com/search.php?queryfor=' + 
encodeURI(queryFor));
            return null;
        }
    };
    return dataService;
}]);


2. In service

myApp..controller('Controller', function($scope, dataService) {
    $scope.results = [];
    $scope.queryFor = "";
    $scope.query = function() {
        dataService.query($scope);
    };
})


myApp.factory('dataService', ['$http', function($http) {
    var dataService = {
        query: function($scope) {
            $http.get('http://www.example.com/search.php?queryfor=' + 
encodeURI($scope.queryFor)).success(function(data, status, headers, config) 
{
                $scope.results = data.results;
            });
            return null;
        }
    };
    return dataService;
}]);

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to