javascript - AngularJs Web 服务

标签 javascript angularjs node.js express webservice-client

我想从此 URL 获取图书名称

http://localhost:3000/greeting

这是我的 Nodejs Expressjs 服务器代码

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!')
});

此代码是我的 Angularjs Web 服务调用,名为 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);
  });
});

这是我的 HTML 代码

<!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">{{book.name}}</h2>    
    </section>
</article>




</body>
</html>

我想获取书名...提前致谢..

最佳答案

您的主app名字是booksInventoryApp所以你应该包括 ng-app="booksInventoryApp"并且每一页都有一个 ng-app指令。

所以尝试改变

 ng-app="demo"

 ng-app="booksInventoryApp"

并删除ng-app="booksInventoryApp"来自<article ng-app="booksInventoryApp">

关于javascript - AngularJs Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44342653/

相关文章:

javascript - 编写此函数的更有效方法? ( Django 、 Ajax )

jquery - 是否应该使用网络服务来创建整个网站?

javascript - 所有内容异步加载后捕获页面

javascript - 在 Node.js 中使用远程图像提供动态生成的 PDF

javascript - 根据文本输入显示和隐藏 DOM 元素

javascript - AngularJS 指令、模板和 ng-include

node.js - 当我启动新进程时,NodeJS fork 进程会减慢所有其他 fork 进程的速度

node.js - 使用 npm install 时权限被拒绝

html - NodeJs 在网络浏览器上显示

javascript - 如何使用具有父子关系的 JSON 数据创建带复选框的树?