Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Angular Service,factory and providers

<!doctype html>
<html ng-app="angular-app2">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
    <script>
        
        var app=angular.module("angular-app2",[]);
        // app.controller('emp',["$scope",function($scope,calcFactory){
        //     $scope.a=67;
        //     $scope.b=89;
        //     $scope.doSum=function(){
        //       $scope.sum=parseInt($scope.a)+parseInt($scope.b);  
        //     };
            
        // }]);
        
        
        app.controller('emp',["$scope",'calcFactory',function($scope,calcFactory){
            $scope.a=67;
            $scope.b=89;
            $scope.doSum=function(){
                //$scope.sum=calcFactory.getSum($scope.a,$scope.b);
               calcFactory.getSum($scope.a,$scope.b,function(result){
                 $scope.sum=result;
                });
                
            };
            
        }]);
        
        app.factory('calcFactory',['$http','$log',function($http,$log){
            $log.log('instantiating calcFactory..');
            var oCalcService={};
            // oCalcService.getSum=function(a,b){
            //     return parseInt(a)+parseInt(b);
            // };
            
           //or
           // oCalcService.getSum=function(a,b,cb){
           //  var s=parseInt(a)+parseInt(b);
           //  cb(s);
           // };
           
           
           //Rest Service
           
           
           
            return oCalcService;
            
        }]);
    </script>
  </head>
  <body>
    <div ng-controller='emp'>
      value of a is {{a}}, Change here <input type="text" ng-model="a"><br>
      value of b is {{b}}, Change it to <input type="text" ng-model="b"><br>
      
      <hr>
      
      Sum is {{sum}}
      <button type="button" ng-click="doSum()">Calculate Sum</button>
    </div>
  </body>
</html>

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.