Fred. Dutra
Fred. Dutra

Reputation: 31

Angularjs Controller return data undefined

I'm trying to write down a Controller that pass a var to a Factory in Angularjs.. The following code return (in console) the values, but I'm not been able to load that into my html page.

Just to record, yes, I'm starting in angularjs.

app.js

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


myApp.factory('eventData', function ($http, $q) {
    delete $http.defaults.headers.common['X-Requested-With'];
    return {
        getEvent: function (id) {
            var deferred = $q.defer();

            $http({
                method: 'GET',
                url: 'page' + id
            }).
            success(function (data, status, headers, config) {
                deferred.resolve(data);
            }).
            error(function (data, status, headers, config) {
                deferred.reject(status);
            });

            return deferred.promise;
        }
    };
});

myApp.controller('AngularJSCtrl',
    function FeederController($scope, eventData) {
        $scope.data = [];
        for (var i = 0; i < 10; i++) {
            eventData.getEvent(i).then(
                function (data) {
                    $scope.data = data;
                    console.log($scope.data);
                },
                function (statusCode) {
                    console.log(statusCode)
                });
        }
    }
);

page.html

<div ng-controller="AngularJSCtrl">
        <div ng-repeat="patient in patients">
            <businesscard>{{patient.name}}</businesscard>
        </div>
    </div>

Upvotes: 0

Views: 525

Answers (1)

Fred. Dutra
Fred. Dutra

Reputation: 31

Problem solved. I've searched for a while until get this right.

Thanks for @Claies and Brad Barrow for the tips :)

app.js

var myApp = angular.module('myApp',[]);
myApp.factory('patientsData', function ($http) {

    delete $http.defaults.headers.common['X-Requested-With'];

    return {
        getPatients: function () {
            return $http({
                url: 'http://localhost/ucamradio/php/tst.php?campusId=1',
                method: 'GET'
            })
        }
    }

});
myApp.controller('AngularJSCtrl', function($scope, patientsData){

    $scope.patients = [];

    var handleSuccess = function(data, status) {
        //$scope.patients = data;
        $scope.patients.push(data);
        console.log($scope.patients);
    };

    patientsData.getPatients().success(handleSuccess);

});

page.html

<div ng-controller="AngularJSCtrl">
        <div ng-repeat="patient in patients">
            <businesscard>{{patient.name}}</businesscard>
        </div>
        <!--   
        <div ng-repeat="patient in patients ">
            <businesscard>{{patient.id}}</businesscard>
        </div> -->
    </div>

Upvotes: 1

Related Questions