vishnu
vishnu

Reputation: 4599

angular directly send $scope parameter from view to service or factory

I have a factory and need to call factory from view. i want to call factory with two parameters. Is it possible to send $scope from template?

Because, i am using same factory for multiple places.

<input name="accnum" ng-blur="myservice.getAccountDetailsToDisplay($scope, accno)" />

controller,

 $scope.myservice= getAllDetailsService;

In service,

tellerApp.factory('getAllDetailsService',['$rootScope', '$resource', '$http', '$filter', '$window',  function ($rootScope, $resource, $http, $filter, $window) {
    return{ 
           getAccountDetailsToDisplay: function ($scope, accountnumber) {
           console.log('>>>>>>');   
          }
};      
}]);

Upvotes: 3

Views: 753

Answers (2)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

Service should be directly depends on scope, they could be indirectly depend on each other. If you pass $scope to the service it will become tightly couple to that specific controller.

Like in your case you only pass accountnumber, then service will make an required operation like doing an ajax call or fetching data from somewhere.

Factory

tellerApp.factory('getAllDetailsService', ['$rootScope', '$resource', '$filter', '$window', function($rootScope, $resource, $http, $filter, $window) {
    return {
        getAccountDetailsToDisplay: function(accountnumber) {
            return $http.get('/getAccountDetails?accountnumber=' + accountnumber).then(function(res) {
                //here you could do your addtional operation on data.
                return res.data; //giving access to data
            });
        }
    };
}]);

Controller

$scope.myservice= getAllDetailsService
//this line will ensure updation in scope
$scope.myservice.accountDetailsToDisplay = getAllDetailsService.accountDetailsToDisplay; 

Markup

 <input name="accnum" ng-blur="myservice.getAccountDetailsToDisplay(accno)"/>

Likewise in above code I didn't use $scope as parameter, service method will only return whatever data is fetched from the service, And whoever the use the service method can get only data return by service. After getting data from service controller modify the scope in its own context.

Upvotes: 2

Alexey Bondarovitch
Alexey Bondarovitch

Reputation: 1

definitely, $scope is inner context of the controllers, so you don't need to use it in any other places. If you want to use factory, you should write like this:

tellerApp.factory('getAllDetailsService',['$rootScope', '$resource', '$http', '$filter', '$window',  function ($rootScope, $resource, $http, $filter, $window) {
    return{ 
           getAccountDetailsToDisplay: function (accountnumber) {
           console.log('>>>>>>');   
    }
  };      
}]);

and in your controller you invoke methods of your factory:

$scope.someMethod = getAllDetailsService.getAccountDetailsToDisplay;

and on your view: <input name="accnum" ng-blur="myservice.someMethod(accno)" />

Upvotes: 0

Related Questions