Angular中的$ http.get请求不起作用

时间:2014-10-27 10:15:08

标签: javascript angularjs

请原谅我,但我是Angular的新手。我在AngularJS中遇到了$ http请求的问题。我究竟做错了什么?几乎和教程一样,但数据仍然没有获取。

var app = angular.module('TestApp', []);
app.controller('TestController', [$http, TestController]);

function TestController($http) {
    var store = this;
    store.personalInfo = [];

    $http.get('/data.json')
    .success(function(data){
        store.personalInfo = data;
    })
    .error(function(){
        store.personalInfo = "Error occured";
    });
}

以下是我的观点:

<body ng-app="TestApp" ng-controller="TestController as test">
    <h1>Hello Plunker!</h1>

    <p ng-repeat="item in test.personalInfo">
        {{ item.firstName }}
    </p>

    <script data-require="angular.js@1.2.25" data-semver="1.2.25" src="https://code.angularjs.org/1.2.25/angular.js"></script>
    <script src="script.js"></script>
</body>

1 个答案:

答案 0 :(得分:0)

您必须使用带引号的注入,更改此:

app.controller('TestController', [$http, TestController]);

由此:

app.controller('TestController', ['$http', TestController]);