使用角度 JS 中的解析对路由进行条件化

Conditionalizing routes with the resolve in angularJS

本文关键字:路由 条件 JS      更新时间:2023-09-26

我正在尝试根据角度的屏幕宽度更改当前路线。我的解析函数在 UI 呈现之前运行,我可以确定是否要显示屏幕的移动变体。

when "/messages/:id",
  templateUrl: "../templates/messages/show.html"
  controller: "MessageCtrl"
  resolve:
    route: ["$location", "Device", "$routeParams", ($location, Device, $routeParams) ->
      console.log($routeParams)
      if Device.screenWideEnough(768)
        console.log "wide enough"
        $location.path "/bigMessageScreen"
        # $location.path("/bigMessagesScreen").search({messageId : $routeParams.id})
    ]

但是,我不确定如何从解析函数中的路由中检索id参数。我已经注入了$routeParams但在检查它时返回了一个空对象。

这样做的"角度方式"是什么?这感觉很笨拙,但我需要检索参数的值,以便我可以正确生成新路由。

$route 服务似乎具有未来路由引用的参数,即使$routeParams没有。以下代码正常运行:

resolve:
    route: ["$location", "Device", "$route", ($location, Device, $route) ->
      if Device.screenWideEnough(768)
        $location.path("/messages").search({id : $route.current.params.id})
    ]