Friday, February 19, 2016

AngularJS Services

The service is a javascript object or a function available to the application. We can use services to wrap the common functionality for the application.

There are my built-in services in Angular JS like $http, $interval, $location etc.

We can define our own services depending on requirements.

<body>

      <div ng-app="myApp" ng-controller="myCtrl">
      
             <p>Result : {{result}}</p>

        </div>

<script>
    
       var app = angular.module('myApp', []);

        app.service('myCalc', function() {

        this.multiply = function (x,y) {
        return x*y;
        }
        this.add = function (x,y) {
        return x+y;
        }

});

app.controller('myCtrl', function($scope, myCalc) {
    $scope.result=  myCalc.add(5,4);
});

</script>

</body>

No comments:

Post a Comment