<!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>
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more