select 下拉菜单和承诺 - 短暂清空 selects

select drop downs and promises - briefly empty selects

我有两个过滤器,在向远程服务器查询数据之前,第一个影响第二个。

我有一个 select,它的选项在 $scope.locationCodes 中。此 select 依赖于另一个下拉列表(客户)。一旦客户被 selected,我就有了 ID,然后将其传递给第一个函数中返回的承诺。

问题一:

一旦我 select 成为客户,如果我真的很快转到 select 位置代码(在代码下拉列表中),选项将是空白的。如果我模糊然后再次尝试 select,选项将出现,表明有一点延迟。在填充数组时处理这种延迟的最佳方法是什么?

代码:

HTML:

<select data-ng-options="locationCode in locationCodes"></select>

JS:

   $scope.getShipAbbleLocations = function (terminalId) {
      var customerId = $scope.customer.id;
      return Service.getShipAbbleLocations(customerId, terminalId).then(function (data) {
        getLocationCodes(data);
        _.defer(function () {
          $scope.$apply();
        })
      });

    };



 function getLocationCodes(data) {
          for (var i = 0; i < data.locationCodes.length; i++) {
            $scope.locationCodes.push(["(" + data.locationCodes[i].shipThruLocationCode + ")", data.locationCodes[i].shipThruLocationId]);
          }
          $scope.$apply();
        }

问题二:

关键是代码下拉列表应该被禁用,直到客户被 selected。我一直依靠我的控制器中的超时来处理禁用,方法是为 'enable' 设置延迟,这允许有时间填充数组。这可行,但一旦您更换客户并且不必担心初始禁用情况,问题就会重新出现。

我想知道是否有比超时更好的方法(希望能更好地理解 angularjs/promises)来处理这个 delay/asynchronicity。

最好的方法是显示 LOADER 并禁用 UI,直到 AJAX 调用完成。

为异步 ajax 调用实施加载程序始终是更好的做法。

您可以在拦截器中实现加载器代码,这样对于每个请求,加载器都会显示出来,您不必为每个 AJAX 调用都实现,因为它在拦截器中。