AngularJS promise

前端 未结 3 1553
别那么骄傲
别那么骄傲 2020-12-10 04:47

AngularJS docs say:

$q promises are recognized by the templating engine in angular, which means that in templates you can treat promises attached to a

3条回答
  •  感情败类
    2020-12-10 05:23

    You need to use the then() function on the promise object:

    this.getItem().then(function(result) {
       $scope.item = result;
    });
    

    In your case I don't think you need a promise. Angular's $watch system will take care of things. Just return an object in your function, not a primitive type:

    this.getItem = function () {
        var item = {};
    
        // do some async stuff
        $http.get(...).success(function(result) {
           item.title = result;
        });
        return item;
    };
    
    $scope.item = this.getItem();
    

提交回复
热议问题