简体   繁体   中英

Using angular $routeProvider with packaged apps

I'm building a Chrome Packaged App with AngularJS and it seems highly desirable to use the $routeProvider however the methods don't seem to match the URL system inside apps.

So my question is can I implement $routeProvider functionality inside a Chrome packaged app and if so how?

Not sure if this is the best solution but I seemed to have solved this enigma myself.

Setup the $routeProvider however you want it to be and instead of using links in the application use ng-click directives that use a the $location to change the path to whatever matches up with your $routeProvider paths.

in the example below I made a "link" directive that sets the $location.path to whatever it equals when clicked.

#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!"

The empty string route matches the initial state of the app.

whatever the link attr is attached to will become a clickable link.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM