Sunday, 11 October 2015

Create a custom Service in Angular Js

In this post we are going to see how to create a service in Angular Js. Service is a singleton object in Anguar, it is created and managed by Angular Dependency Injection.


app.service('DBService'function () {

    this.ConnectionString = function () {
        return "DataSource=123.23.2.11;Username=rajesh;Password=pass12*";
    }

    this.Login = function (username, password) {
        if (username == "Rajesh") {
            return true;
        }
        else {
            return false;
        }
    }

});


var app = angular.module('app', []);

app.controller('main'function ($scope, DBService) {

    $scope.Title = "Sample Angular Application";
    $scope.Cstring = DBService.ConnectionString();
   

});





<html>
<head>
    <script src="scripts/angular.min.js" type="text/javascript"></script>
    <script src="scripts/angular-route.min.js" type="text/javascript"></script>
    <script src="app.js" type="text/javascript"></script>
</head>
<body>
    <div ng-app="app">
        <div ng-controller="main">
            <h1>{{Title}}</h1>
            <br />
            <span> Connection String from Service: {{Cstring}} </span>
            <br />          
        </div>
    </div>
</body>
</html>



from this post you can learn how to create a service in angular js.



No comments:

Post a Comment