Scope of $scope must remain in Controller. Service must not update the
scope rather provide the value from DB/Web-Services, which will be updated
in $scope by controller.
On Wednesday, 12 November 2014 15:45:17 UTC+5:30, 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.