1

我正在使用Angular 1.4.1。 我做了一个指令,一个带有缩略图和链接的通用卡片。该链接是一个通用的绝对网址,但通常会导致应用程序内的某个地方。AngularJS-href在数据准备好之前重定向到相同的域导致页面显示

因此,假如我在https://stackoverflow.com/section/有可能是带有链接的卡片状https://stackoverflow.com/section/detail

我有一个app.js类似这样(我只是张贴了有关路段)

.state('base.section', { 
 
      url: 'section/', 
 
      views: { 
 
       '@': { 
 
        templateUrl: 'views/section.html', 
 
        controller: 'SectionController' 
 
       } 
 
      }, 
 
      resolve: { 
 
       'items': function (SomeService) { 
 
        console.log("items resolve triggered"); 
 
        return SomeService.doThingsReturnPromise(); 
 
       } 
 
      } 
 
} 
 
.state('base.section.detail', { 
 
      url: 'detail', 
 
      views: { 
 
       '@': { 
 
        templateUrl: 'views/detail.html', 
 
        controller: 'DetailController' 
 
       } 
 
      }, 
 
      resolve: { 
 
       'items': function (items) { 
 
        return items; 
 
       }, 
 
       'stuff': function(SomeOtherService) { 
 
        console.log("stuff resolve triggered."); 
 
        return SomeOtherService.doThingsReturnPromise(); 
 
       } 
 
      } 
 
} 
 
       

所以问题是,当我点击

<a ng-href="https://stackoverflow.com/section/detail">

我得到正确重定向到详细信息页面,但在解决了之前的承诺已完全解决,我得到所有的角像显示的变量({{somethingLikeThis}})。

即使解决方案被触发(我可以在控制台中看到日志),似乎只是Angular在加载控制器之前不会等到解析完成,因为它应该表现正常。

当然,如果我重新加载详细信息页面,我会得到所有的值完全加载。如果我移动到另一个网站部分(https://stackoverflow.com/baloons),也是如此。

这个问题似乎是,当我从“节”,以“细节”移动到刚刚发生,从相同的运行应用程序

希望能提供了一个很好的问题,解释

UPDATE

我解决了这个问题,但为了更好地理解我想指定服务功能。他们是这样的

angular.module('myApp') 
 
.factory('SomeService', 
 
function ($resource, serverURL, protocol, requestFormat) { 
 
     return $resource(serverURL + 'backendUrl/', 
 
      { 
 
       callback: 'JSON_CALLBACK', 
 
       format: requestFormat 
 
      }, 
 
      { 
 
       doThingsReturnPromise: { 
 
        method: protocol, 
 
        isArray: true 
 
       } 
 
      }); 
 

 
});

只是为了完整性,原来,这个问题即使使用$ q.all(承诺)的决心

回答

1

的问题解决了使用。$承诺在决心(没有双关语意)

resolve: { 
 
    'items': function (SomeService) { 
 
     console.log("items resolve triggered"); 
 
     return SomeService.doThingsReturnPromise().$promise; 
 
    }

我有$ q.all同样的问题和解决方案是一样的,只是略有不同

resolve: { 
 
    'items': function (SomeService, SomeOtherService, $q) { 
 
     var promises = [ 
 
      SomeService.doThingsReturnPromise().$promise, 
 
      SomeOtherService.doThingsReturnPromise().$promise 
 
     ]; 
 
     return $q.all(promises); 
 
    } 
 
}

$ q.all返回一个承诺,因此它可以不指定。 $诺言,但服务请求必须

相关问题