Monday, 3 October 2016

Angular JS CRUD - DB

Service.js

/// <reference path="D:\Projects\testapi\testapi\Scripts/angular.min.js" />


app.service('personservice', function ($http, $location) {

    this.personlist = function () {
        return $http({
            method: "GET",
            url: "/api/people"

        }).then(function (result) {
            return result.data;
        });
    };

    this.addPerson = function (name, age) {

        return $http({
            method: "POST",
            url: "/api/People",
            data: { name: name, age: age }
        }).then(function (result) {
            return result.data;
        })
    };

    this.deletePerson = function (id) {
        return $http({
            method: "delete",
            url: "api/People/" + id,
        }).then(function (result) {
            return result.data;
        })
    };

});


Controller.js

/// <reference path="D:\Projects\testapi\testapi\Scripts/angular.min.js" />

app.controller('personcontroller', function ($scope, personservice, $location) {

    init();
    function init() {
        personservice.personlist()
        .then(function (result) {
            $scope.people = result;
        });
    }

    $scope.addPerson = function () {
        var name = $scope.newPerson.name;
        var age = $scope.newPerson.age;
        personservice.addPerson(name, age)
           .then(function (result) {
               $scope.addedPerson = result;
           });
    }

    $scope.deletePerson = function (id) {
        personservice.deletePerson(id)
            .then(function (result) {

                $scope.deletedPerson = result;
            });
    };

});



App.js
/// <reference path="" />
var app = angular.module('app', ['ngRoute']);

app.config(function ($routeProvider) {
    $routeProvider.when('/add',
        {
            controller: 'personcontroller',
            templateUrl: 'AngularCRUD/Person/views/add.html'
        })
    .when('/edit/:id', {
        controller: 'personcontroller',
        templateUrl: 'AngularCRUD/Person/views/edit.html'
    })
    .when('/list', {
             controller: 'personcontroller',
             templateUrl: 'AngularCRUD/Person/views/list.html'
         }).
    otherwise({ redirectTo: '/list' });

});


No comments:

Post a Comment

Bevereages

Types of Beverages Fermented - yeast reacts with sugar to convert into ethyl alcohol & CO2. 4-14% Wine (red, white & rose), Cider ...