2016-05-17 15 views
0

我打电话给ng-click这个函数。Angularjs:空物体,只有一次点击时?

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 
} 

回答

1

您正在发出HTTP请求,我认为问题在那里。当您首次通过点击呼叫您的功能时,您的呼叫正在处理中,并且您的功能一直在继续。

这就是为什么当你点击一次,在你的函数结束时,你的对象是空的。一旦你的物体被加载 - 小人物说,在你第二次点击 - ,你会最终收到它。你应该做的是等待你的HTTP呼叫在处理之前结束。你可以用肮脏的方式($ timeout)或者寻找像this one这样的解决方案来提供有用的信息。

+0

我想那可能就是这么说的! –

+0

希望它帮助你解决问题:) –

+0

完美这个$超时是超级:) –