代码之家  ›  专栏  ›  技术社区  ›  JWP

AngularJS如何在Promise返回时更新GUI

  •  0
  • JWP  · 技术社区  · 7 年前

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

    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 = [];             
            });
    }
    

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

    异步更新从不显示,但所有其他字段都显示在GUI中。GUI不应该在每个承诺完成时自动更新吗?我需要做手术吗$范围。应用()[这也不管用]还是我需要把每件事都从$范围.会议并重置为新的已解析的promise对象?

    我很困惑,请帮帮我。

    3 回复  |  直到 7 年前
        1
  •  1
  •   lealceldeiro VonC    7 年前

    您正在将“$scope对象”传递给 getDetails() getDetails 函数并更新这个本地对象而不是$scope对象。

    所以,你应该和我一起去[ (your $scope object).details = result.details thing.details = result.details ].

        2
  •  0
  •   JWP    7 年前

    我的最终答案完全不同。结果发现另一个组件已经发出了我需要的异步查询。我只是在那里放了一个发射器,等着答案出来。。。(听起来像RXjs)。

    但是要让更新的记录显示出来,我必须在数组中找到记录,更新它,然后更新数组记录。这样地:

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

    作为digest循环的新手,我认为仅仅设置属性就足以检测到更改并重新呈现。

    请评论。

        3
  •  -1
  •   Mr. Strike    7 年前

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

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

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