Angular $ routeProvider - 以编程方式添加路由解析

时间:2015-11-03 20:01:39

标签: javascript angularjs angular-ui-router

我想在应用程序定义后动态添加Angular resolve。想象一下,我的应用程序定义了这样的路线:

app.config(['$routeProvider', function ($routeProvider) {
  $routeProvider
    .when("/", {templateUrl: "partials/home.html", controller: "PageCtrl"   })
    .when("/services", {templateUrl: "partials/services.html", controller: "PageCtrl"   })
}]);

然后我想添加/编辑/覆盖特定路线到resolve承诺:

app.config(['$routeProvider', function ($routeProvider) {
  $routeProvider
    .when("/services", {templateUrl: "partials/services.html", controller: "PageCtrl",  ,resolve:{getData: function(myService){return myService.getOffer();}}    })
}]);

我该怎么做?这里的关键是在定义/配置后添加。所以这段代码将在未来某个时候运行。

1 个答案:

答案 0 :(得分:1)

app.run(function ($route) {
  var route = $route.routes['/...'];
  route.resolve = route.resolve || {};
  route.resolve.someResolver = ...;
});