AngularJS在用户点击范围内等待$resource承诺

AngularJS Waiting for $resource Promise in the Scope of a User Click

本文关键字:等待 resource 承诺 范围内 用户 AngularJS      更新时间:2023-09-26

我是前端/AngularJS开发的新手,并且已经阅读(但不完全理解)许多堆栈溢出问题和其他有关ng-resource/$resource和承诺的文档。我的情况有点奇怪。

我在提交函数中有此代码,当用户单击按钮提交表单时。

        var Resource = $resource("/urlHere",{},{save: {method:'POST', isArray:true}});
        Resource.save(JSON.stringify($scope.validQuery)).$promise.then(function(data) {
        $scope.initialResults=data;
        if($scope.initialResults.length === 0){
           alert("No Results Found!");
        }else if($scope.initialResults.length === 1){
            window.data = $scope.initialResults;
            window.open('/detailedResults');
       }else if($scope.initialResults.length > 1){
            window.data = $scope.initialResults;
            var popupWindowResults = window.open('/resultsGrid');
        }
      });

我需要打开某个选项卡,具体取决于我得到的结果数量。(基本上显示一个有多个结果可供选择的网格,或者如果只返回 1,则只显示单个结果)当 window.open 不包含在 $promise.then 中时,它可以正常工作。在 $promise.then 中,浏览器将其视为弹出窗口,而不是用户单击某些内容后的新选项卡。

有没有办法在返回结果之前停止执行,以便我可以在 $promise.then 之外和提交函数中打开一个选项卡?任何解决方案都将非常有帮助。

http://jsfiddle.net/az066xcs/2/

$scope.getResults = function() {
    var win = window.open();
    // this is a placeholder for the async call that gets results
    setTimeout(function() {
        win.document.write('<h1>Results Loaded!</h1>');
    }, 1000);
};

您可以在单击时打开窗口,然后异步加载结果并将其插入新窗口中。显然,比带有document.write的空白窗口更好的解决方案是在新窗口中放置另一个角度应用程序,或者至少是一个简单的javascript应用程序。