使用带有打包应用程序的angular $ routeProvider

时间:2013-09-14 01:08:10

标签: angularjs google-chrome-app

我正在使用AngularJS构建Chrome打包应用程序,并且似乎非常希望使用$ routeProvider,但这些方法似乎与应用程序内的URL系统不匹配。

所以我的问题是我可以在Chrome打包的应用程序中实现$ routeProvider功能,如果是这样的话?

1 个答案:

答案 0 :(得分:0)

不确定这是否是最佳解决方案,但我似乎已经解决了这个谜团。

设置$ routeProvider然而你想要它而不是使用应用程序中的链接使用ng-click指令使用$ location来改变与$ routeProvider路径匹配的路径。

在下面的示例中,我创建了一个“link”指令,将$ location.path设置为单击时等于的值。

#coffescript:
app.directive "link", ($location) ->
    (scope, element, attrs) ->
      element.bind "click", ->
        scope.$apply $location.path(attrs.link)

app.config ($routeProvider) ->
  $routeProvider

    .when "",
      templateUrl: "index.html"

    .when "/otherPage",
      templateUrl: "path/to/otherPage.html"

    .otherwise
      template: "Fail!"

空字符串路由与应用的初始状态匹配。

无论附加链接attr将成为可点击链接。

<button link="otherPage">Take me to otherPage</button>
<div link="otherPage">Make divs clickable too why not?</div>