Saturday, 3 June 2017

AngularJs Web service

I want to get books name from this URL

http://localhost:3000/greeting

This is my nodejs Expressjs Server Code

var express = require('express');
var app = express();

app.get('/greeting', function (req, res) {

var books = {
    id : 555,
    name : 'Antony',
    title : 'Do or Die',
    price : {
        inr : 500,
        dollar : 5
        }
    };
    res.json(books);
});

app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});

This code is my Angularjs webservice call named as hello.js

var app = angular.module('booksInventoryApp', []);
app.controller('booksCtrl', function($scope, $http) {
$http.get("http://localhost:3000/greeting")
   .then(function(response) {
  $scope.data = response.data;
  console.log("result"+response.data);
  });
});

This is My HTML code

<!doctype html>
<html ng-app="demo">
<head>
    <title>Hello AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script src="hello.js"></script>
</head>

<body>

<article ng-app="booksInventoryApp">
    <section ng-controller="booksCtrl">
        <h2 ng-repeat="book in data.books"></h2>    
    </section>
</article>




</body>
</html>

I want to GET Name of Book... Thanks in advance..



via Ram Android

No comments:

Post a Comment