Monday, 6 June 2016

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=;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();


    <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>
    <div ng-app="app">
        <div ng-controller="main">
            <br />
            <span> Connection String from Service: {{Cstring}} </span>
            <br />          

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

No comments:

Post a Comment