Angularjs 和网络服务

Angularjs and web services

<div ng-app="myapp" ng-controller="hello">

<script>            
function Hello($scope, $http) {
    $http.get('http://rest-service.guides.spring.io/greeting').
    success(function(data) {
        $scope.greeting = data;
    });
}
</script>   

我想打印这样的内容{{greeting.id}}。 该代码片段将使用 angularjs 从“http://rest-service.guides.spring.io/greeting”获取 JSON 格式的数据。这在日食中不起作用。有什么想法可以让它发挥作用吗?

我相当确定 ng-controller 的值区分大小写。

所以,这个:

ng-controller="hello"

将查找名称为 hello 的函数,而不是 Hello.

Here is a plunker 演示问题。

像这样更改您的控制器代码。

(function () {
  var myApp = angular.module('myModule');

  myApp.controller('hello', ['$scope', '$http', function ($scope, $http) {

       $http.get("http://rest-service.guides.spring.io/greeting").then(function(data){
       $scope.greeting=data;
       });

  }]);
})();

并在 html

{{greeting.data.id}}

我在plunker里查过了。工作正常