Angularjs:空对象,当只有一次点击时

Angularjs:Empty object, when have only one click?

本文关键字:一次 对象 Angularjs      更新时间:2023-09-26

这个函数我在点击时调用。

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

这个函数我想从$scope.scheduleid得到结果,但这里有问题。当我一次点击$scope.scheduleid(空),但两次或多次点击$scope.scheduleid(没有空并获取数据)时。为什么我的第一次点击是空的。

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

您正在发出HTTP请求,我认为问题就在那里。当你第一次通过点击调用你的函数时,你的调用正在处理中,你的函数继续运行

这就是为什么当你在函数末尾点击一次时,你的对象是空的。一旦你的对象被加载,比如说,在你第二次点击时,你最终会收到它。你应该做的是等待HTTP调用结束然后再处理。你可以用肮脏的方式($timeout)来做这件事,或者看看像这样的解决方案,它应该提供有用的信息。