Angular sends $ scope parameter directly from view to service or factory

I have a factory and need to call the factory from the view. I want to call a factory with two parameters. Can I send $ scope from a template?

Because I am using the 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('>>>>>>');   
          }
};      
}]);

      

+3


source to share


2 answers


The service should be directly dependent on scope

, they may be indirectly dependent on each other. If you pass $ scope to a service, it becomes tightly coupled with that particular controller.

As in your case, you only pass accountnumber

, then the service will perform the required operation, such as making 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 the above code, I did not use a parameter $scope

as a parameter, the service method will only return the data that is retrieved from the service, and whoever uses the service method can only receive data returned from the service. After getting the data from the service controller, change the scope in your own context.

+2


source


Definitely, $ scope is the internal context of the controllers, so you don't need to use it elsewhere. If you want to use a 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 are calling the methods of your factory:



$scope.someMethod = getAllDetailsService.getAccountDetailsToDisplay;

      

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

0


source







All Articles