Angularjs:Empty object, when have only one click?

This function i call on ng-click.

HTML

<div class="col-md-offset-10">
    <div class="form-group">
    <a class="btn btn-default" ng-click="schedule ()">Add</a>
    </div>
</div>

JS

$scope.scheduleid={};
var inc=0;

$scope.schedule = function ()
{   
    inc += 1;
    console.log($scope.from);

    console.log ($scope.dateObj);
    if (inc == 1)
    {
        var schedule = {}
        schedule._type = "Schedule";

        console.log($scope.dateObj);
        var insertSchedule = BasicJSONCreator.basicEdit ();
        insertSchedule.Calls[0].Arguments = [];
        insertSchedule.Calls[0].Arguments[0] = schedule;

        httpRequester.async (insertSchedule).then (function (data) {

        console.log(data.data.ReturnValues[0].scheduleID);
        $scope.scheduleid=data.data.ReturnValues[0] //This get response from my database
        console.log($scope.scheduleid ); // here object is get my data from database

        });
    }
    $scope.getData();
}

This funciton i want get result from $scope.scheduleid, but here is problem. When i have one click $scope.scheduleid (empty), but two and more click $scope.scheduleid (no empty and get data).Why my first click is Empty.

$scope.getData=function()
{
    console.log($scope.scheduleid)
    // Here my object is empty when have one click,but two and more click object no empty and get data
}

Upvotes: 0

Views: 101

Answers (1)

Yassine Badache
Yassine Badache

Reputation: 1851

You are making an HTTP request, I think the problem is there. When you first call your function through the click, your call is being processed, and your function keeps going on.

That's why when you click once, at the end of your function, your object is empty. Once your object is loaded -let's say, at your second click-, you will finally receive it. What you should do is wait for your HTTP call to end before processing. You can do this the dirty way ($timeout) or looking at solutions like this one which should provide useful informations.

Upvotes: 1

Related Questions