I think that the key point in Todd Motto's frame of reference (which is
more-or-less how I've been doing newer angular stuff) is like:
Services handle the $http request and abstract all that behind an object
facade. So you do stuff like var Potato = new Tuber('potato'). Then in the
constructor (or whatever other way you like doing it), the Tuber service
calls $http.get and handles promise resolution to this.skinTexture and so
forth.
In the controller, if you use controllerAs syntax, you can do stuff like
this.myTuber = new Tuber($stateParams.tuberName).
The gist of it is still - deal with the $scope in the controller, or use
the new 'controller as' syntax to avoid using the $scope directly (treat
the controller as an object you can bind scope params to this.param on).
Abstract dealing with the $http interface and parsing results into a Model
object which knows how to instantiate itself, and use controller objects to
create Model objects and attach them to the scope.
e
On Fri Nov 14 2014 at 4:51:25 AM Joberto Diniz <[email protected]>
wrote:
> I think is very weird passing $scope to a service. Services shouldn't
> manipulate scopes.
>
>
> On Wednesday, November 12, 2014 8:15:17 AM UTC-2, John Chacko 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.