2016-08-05 55 views
-1

嗨我已经阅读了许多关于承诺,解决承诺和访问数据的帖子,但我似乎无法看到。在这里的一些帖子后,刚刚引起错误,所以我不知道我到底做错了什么。Angular JS 1.X访问已解决Promise对象值

我有一个功能,像这样:

function getJsonl() { 

    var deferred = $q.defer(); 
    $http({ 
    url: 'urlNotShownForSecurity', 
    dataType:"json", 
    method: 'GET', 
    data:{"requestId":"123"}, 
    headers:{"Content-Type":"application/json","requestId":"123"}, 
    }).success(function(data) { 
    deferred.resolve(data); 
    console.log(data) 

    }).error(function(error,status,headers,config) { 
    deferred.reject(error); 

    }); 

    return Promise.resolve(deferred.promise); 
} 

在这里,我返回已经解决导致JSON对象,我相信一个JSON承诺。

打印到控制台我得到如下:

enter image description here

里面的数据是我需要的信息,它看起来像这样:

data:Array[8] 
    0:Object 
    description:"My description paragraph" 

我已经试过的东西,在返回的对象我的控制器如: vm.result = data.data [0] .description; vm.result = data [0] .description

我已经尝试了很多不同的方法在视图以及访问,但我得到2空白锂标签,就是这样。

我想能够访问数据,所以我填充表。因此,如果我可以使用它与ng重复,这将是伟大的,以及能够访问没有,因为一些数据不仅仅是在表中使用。

任何帮助,非常感谢。谢谢

@DanKing 执行后,我在控制台中得到以下输出: 现在我回来了一个promise对象。

enter image description here

+0

仅供参考,'success'和'error'方法最近'$ http'实现弃用。即使你的角度仍然支持他们,最好用未来的眼光来编码。 – Mike

+0

可以理解。 – L1ghtk3ira

回答

1

在我看来好像你从根本上误解了诺言的性质。

$ http()是一个异步函数 - 这意味着它不会马上完成,这就是它返回promise的原因。

它在我看来好像你试图调用$ http(),然后得到结果并返回它从你的getJson1()方法, $ http()已完成执行之前。

你不能那样做。你getJson1()方法应该原封不动地返回的承诺,所以你的调用方法可以链接到它 - 这样的:

getJson1().then(function(data) { 
    // do some other stuff with the data 
}); 

承诺链的整点是,他们不执行通俗易懂 - 而不是你提供的回调函数当以前的操作完成时,将在未来的某个不确定点执行。

你getJson1()函数只需要做到这一点:

return $http({ 
    url: 'urlNotShownForSecurity', 
    dataType:"json", 
    method: 'GET', 
    data:{"requestId":"123"}, 
    headers:{"Content-Type":"application/json","requestId":"123"}, 
}); 
+0

这解释了为什么在控制台中它有2个未定义的而不是承诺对象。你能用我的json1写一个例子吗?您的小版本我不完全确定如何实施您的最后一段。也不会promise.resolve修复它呢? – L1ghtk3ira

+0

好的,我已经扩展了我的答案。 Promise.resolve()是ES6标准,AFAIK不适用于Angular promise。但即使这样也不会对你有所帮助,而且我还不清楚你想用它做什么。 –

+0

我正在测试该解决方案。这是问题的一半。另一半是如何访问对象中的值。使用像ng-repeat这样的指令,例如 – L1ghtk3ira

-1

好了,改写了答案作为一个完整的组件,以显示移动部件。

$ http返回一个承诺,以便您的原始getJsonl呼叫可以简化。使用你原来的$ HTTP参数如果您使用<json-thing>标签这应该转储到屏幕上你的API响应:

angular.module('yourModuleName') 
.component('jsonThing', { 
    template: '<pre>{{$ctrl.thing|json}}</pre>', 
    controller: function ($http) { 
    var $ctrl = this; 
    getJsonl() 
     .then(function (response) { 
     console.log(response); // we have the $http result here 
     $ctrl.thing = response.data; // allow the template access 
     }, function (error) { 
     console.log(error); // api call failed 
     }); 

    // call backend server and return a promise of incoming data 
    function getJsonl() { 
     return $http({ 
     url: 'urlNotShownForSecurity', 
     dataType: 'json', 
     method: 'GET', 
     data: { requestId: '123'}, 
     headers: { 'Content-Type': 'application/json', requestId: '123'} 
     }); 
    } 
    } 
}); 
+0

使用承诺与解决我有它的数据显示在控制台日志中,但我不能单独或作为一个整体在html中访问它 – L1ghtk3ira

+0

你不需要额外的'defer'和'promise'的额外复杂性。但是如果你坚持,那么你应该在'getJsonl'方法中返回_promise_并且_不解决它!决议发生在你的“成功”方法中。 – Mike

+0

我很困惑。你能举一个例子吗?从我阅读的内容中我需要解析方法 – L1ghtk3ira

-1
getJsonl().then(function(data){ 
    console.log(data); 
},function(err){ 
    console.log(err); 
}) 

应该工作。您的$ http请求在哪里以及您对getJsonl()的调用在哪里也会产生影响。所以在实施时请谨慎选择。如果您在服务中使用这个,那么你将不得不返回函数结果说

this.somefunction = function(){ 
    return getJonl(); 
} 

,并在控制器中注入服务,请执行下列操作

service.somefunction().then(function(data){ 
     console.log(data); 
    },function(err){ 
     console.log(err); 
    }) 
+0

它在一个工厂绑定到绑定到一个html页div元素的控制器 – L1ghtk3ira

+0

我的http请求是在getjson方法 – L1ghtk3ira

+0

更新了更改尝试代码 – Gary