AngularJS Promise返回时如何更新GUI

时间:2018-10-02 17:20:04

标签: angularjs

我的视图绑定到完美呈现的对象集合。属性之一是异步,异步返回时不会在GUI中更新该值。为了启动异步工作,我映射了要显示的每个属性,将async属性设为零,然后执行以下操作:

var date = JSON.parse(<?php echo JSON.stringify($date_array); ?>);
var daylight = JSON.parse(<?php echo JSON.stringify($daylight_array); ?>);

事物是GUI使用的$ scope.details绑定的一部分。

function getDetails(thing) {
    console.log("---->" + thing.id);
    thingService
        .getDetailsAsync(thing.id, true)
        .then(function Success(result) {
            thing.details = result.details;                                   
        }, function Error(reason) {
            thing.details = [];             
        });
}

异步更新从不显示,但其他所有字段都在GUI中显示。完成每个承诺后,GUI是否不应该自动更新?我需要做一个$ scope.Apply()还是没用?还是我需要从$ scope.meeting中“拔出”所有东西并重置为新的已解决的Promise对象?

我很困惑,请帮忙。

3 个答案:

答案 0 :(得分:1)

从您分享的内容中,我所得到的就是, 您将“ $ scope object”作为参数传递给getDetails()函数,该参数基本上在JS中为getDetails函数创建了一个新的本地对象“ thing”,并更新了该本地对象而不是$ scope对象。

因此,您应该使用[(your $scope object).details = result.details]代替[thing.details = result.details]。

答案 1 :(得分:0)

我的最终答案是完全不同的。事实证明,另一个组件已经在发出我需要的异步查询。我只是把一个发射器放在那里,等待答案出来……(听起来像RXjs)。

但是要显示更新的记录,我必须在数组中找到记录,对其进行更新,然后再更新数组记录。像这样:

var found = _.find(details, { id:id }
if(found){ found.notes = emittedData;}
found = found; <--This fired of the re-render with new data.

作为摘要周期的新手,我以为设置属性就足以检测到更改并重新渲染。

请发表评论。

答案 2 :(得分:-1)

map返回一个新数组。 使用forEach,例如

$scope.meeting.forEach(thing=>{
    getDatails(thing);
});

或者如果您只想使用地图

$scope.meeting = $scope.meeting.map(thing=>{
     getDatails(thing);
});