简体   繁体   中英

$http.get requests in Angular doesn't work

Forgive me, but I'm newbie in Angular. I've got a problem with $http requests in AngularJS. What am I doing wrong? Almost as in tutorial, but data still aren't fetch.

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";
    });
}

And here is my view:

<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>

You must use Injection with quotes, change this:

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

By this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM