Personally, I do it in the controller. I have a hard-and-fast rule: $scope
manipulation is in the controller. So the service method will return a
promise that resolves to the data, and the controller attaches the data to
the scope.

Note, you can probably also pass {params: {queryfor: queryFor}} to the
$http get call as a second argument and not worry about string
interpolation (just pass the '...search.php' path as the first argument).

e


On Wed Nov 12 2014 at 2:16:14 AM John Chacko <[email protected]> wrote:

> 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) {
>
>             return $http.get('http://www.example.com/search.php?queryfor='
> + encodeURI(queryFor));
>         }
>     };
>     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.
>

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