CoffeeBean CoffeeBean - 19 days ago 5
Javascript Question

How to make sure that $scope.property has value before it is used

I have an Angular controller with two $scope properties:

$scope.details = ortabilityService.detail();

$scope.treeViewOptions = {
dataSource: new kendo.data.HierarchicalDataSource({
data: $scope.details
};


As we can see, tree view options depend on details to be resolved before it uses it as data source. Now, the problem is that it is an Async call and it is always not guarantee that it is resolved before it is used inside treeviewOptions. Because of this it is a very unreliable load and it loads sometimes and does not load some times. How can I make sure that $scope.details has value before it is used inside treeViewOptions?

Answer

If detail() returns a promise, you can do this:

ortabilityService.detail().then(function(result) {
  $scope.details = result;

  $scope.treeViewOptions = {           
    dataSource: new kendo.data.HierarchicalDataSource({
      data: $scope.details               
    })
  };
});