AngularJS 承诺返回空对象 [英] AngularJS promise returning empty object

查看:30
本文介绍了AngularJS 承诺返回空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想要做的是从解决承诺的函数调用中分配一些模型值.像这样

value = someFun()

这是我调用此函数的服务

app.factory('SomeService', function($q) {返回 {someFun:函数(){var d = $q.defer();尝试 {d.resolve("你好");}赶上(e){d.拒绝(e);}返回 d.promise.then(function(text){返回文本;});}};});

这是HTML代码

<pre>{{value |json}}</pre>

<button type="button" ng-click="value = someFun()">点击我</button>

这是在控制器中

$scope.someFun = SomeService.someFun;

这里是plnkr

http://plnkr.co/edit/qO5ofBXZDsi3cS3bBnT8

现在它返回一个空对象.怎么了?

正如下面已经回答的那样,是的,这是一种方式,但假设我想在 ngRepeat 中调用 SomeService.someFun?

这是答案 -> Angular UIngRepeat 内的 Bootstrap 模态

解决方案

通常,如果您想使用异步更新的内容出现在视图中,而不必编写控制器代码(在您的问题中就是这种情况,因为您想在 ng-view 中调用该函数),那么您需要返回一个空白的占位符对象,并将承诺的结果合并到其中,或者更新您可以在视图中引用的该对象的属性.

您的问题太抽象了,无法知道解决这个问题的确切方法对您的应用程序有意义,但这里是一般(和抽象)的想法 plunker 在这里:

.factory('promiseService', function($q){返回 {someDeferredThing:函数(){var 占位符 = {};var d = $q.defer();d.promise.then(函数(文本){placeholder.text = 文本;});d.resolve("你好");返回占位符;}})

服务返回一个由承诺更新的对象,而不是承诺本身.如果你返回一个 promise,那么你必须处理 .then() 等来获取值.

.controller('appCtrl', function($scope, promiseService){$scope.deferredResult = promiseService.someDeferredThing();});

这就是您将值分配给范围的方式.

$scope.deferredResult.text 将是 undefined 直到 promise 解析,然后它会被定义并解析为 "text".因此,您的视图应该在短时间内处理未定义状态 - 通常使用 ng-show 来做到这一点非常简单.

Basically what I want to do, is to assign some model value from function call that resolves promise. Like so

value = someFun()

This is a service from which I call this function

app.factory('SomeService', function($q) {
    return {
        someFun: function() {
            var d = $q.defer();
            try {
                d.resolve("hi");
            } catch (e) {
                d.reject(e);
            }
            return d.promise.then(function(text){
                return text;
            });
        }
    };
});

Here is the HTML code

<div ng-init="value = 'yes'">
    <pre>{{value |json}}</pre>
</div>
<button type="button" ng-click="value = someFun()">click me</button>

And this is in the controller

$scope.someFun = SomeService.someFun;

Here is plnkr

http://plnkr.co/edit/qO5ofBXZDsi3cS3bBnT8

Right now it returns an empty object. What is wrong?

EDIT: As already answered below, yes this is one way, but let's say I want to call SomeService.someFun in ngRepeat?

EDIT2: HERE IS THE ANSWER -> Angular UI Bootstrap modal inside ngRepeat

解决方案

Generally if you want to use something that is updated asynchronously to appear in a view without having to write controller code as well (which is the case in your question, as you wanted to call the function inside ng-view), then you need to return a blank placeholder object, and either merge the result of the promise into it, or update a property of that object which you can reference in the view.

Your question is too abstract to know the exact way to approach this to make sense for your application, but here is the general (and abstracted) idea plunker here:

.factory('promiseService', function($q){
  return {
    someDeferredThing: function(){
      var placeholder = {};
      var d = $q.defer();
      d.promise.then(function(text){
        placeholder.text = text;
      });
      d.resolve("hi");
      return placeholder;
    }
  }
)

The service returns an object which is updated by the promise, and not a promise itself. If you return a promise then you have to deal with .then() etc. to get the value.

.controller('appCtrl', function($scope, promiseService){
  $scope.deferredResult = promiseService.someDeferredThing();
});

This is how you assign the value to the scope.

$scope.deferredResult.text will be undefined until the promise resolves, then it will become defined and resolve to "text". As such, your view should handle the undefined state for a short duration - usually it's very simple to do that with ng-show.

这篇关于AngularJS 承诺返回空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆