angularjs - angular directly send $scope parameter from view to service or factory -
i have factory , need call factory view. want call factory 2 parameters. possible send $scope template?
because, using same factory 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('>>>>>>'); } }; }]);
service should directly depends on
scope
, indirectly depend on each other. if pass $scope service become tightly couple specific controller.
like in case pass accountnumber
, service make required operation doing ajax call or fetching data 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 addtional operation on data. return res.data; //giving access data }); } }; }]);
controller
$scope.myservice= getalldetailsservice //this line ensure updation in scope $scope.myservice.accountdetailstodisplay = getalldetailsservice.accountdetailstodisplay;
markup
<input name="accnum" ng-blur="myservice.getaccountdetailstodisplay(accno)"/>
likewise in above code didn't use $scope
parameter, service method return whatever data fetched service, , whoever use service method can data return service. after getting data service controller modify scope in own context.
Comments
Post a Comment