Whirlwind990 Whirlwind990 - 1 day ago 4
AngularJS Question

Angular http get not working

I'm doing some testing with an API in apiary.io and I wanted to call some data from it with Angular, but it doesn't seem to call correctly. It should be pretty simple, so I'm not sure whats wrong:

HTML:

<span><a href="network-updates.html">{{user.text}}</a></span>


JS:

var MainController = function($scope, $http){

var usercomplete = function(response){
$scope.user = response.data;
};

$http.get("http://private-abc123-.apiary-mock.com/bus")
.then(usercomplete);

};


JSON:

{
"Header" : "heading",
"text" : "hello"
}

Answer

This works for me out of the box try: (This solution is no longer correct, see update below)

var a = angular.module('a', []);

a.controller('dbCtrl', ['$scope', '$http', function ($scope, $http) {

$scope.loadData = function () {
    $http.get("url")
        .success(function(data){
            $scope.data = data; //return if success on fetch
        })
        .error(function() {
            $scope.data = "error in fetching data"; //return if error on fetch
        });
    };

    $scope.loadData(); //return loadData function

}]);

Update:

var a = angular.module('a', []);

a.controller('dbCtrl', ['$scope', '$http', function ($scope, $http) {

$scope.loadData = function () {
    $http.get("url")
        .then(function(data){
            $scope.data = data; //return if success on fetch
        }, function() {
            $scope.data = "error in fetching data"; //return if error on fetch
        });
    };

    $scope.loadData(); //return loadData function

}]);
Comments