Wednesday 15 June 2011

AngularJS $location.path() changed after upgrading to 1.1.15 -


I have a navigation controller in which the navigation list is the selected item for the currently selected item. This is $ location.path () Tries to use and sets it to the default value There is a simplified version in this hashbang mode:

  App.controller ("Navigation Controller", ['$ scope', '$ location', function ($ radius, $ location) { $ Scope.currentSelection = $ Location.path () || "/ dashboard"; $ scope.select = function (name) {$ scope.currentSelection = name;}}]));   

and html:

  & lt; Body ng-app = "app" & gt; & Lt; Div class = "container-fluid absolute" & gt; & Lt; Div class = "row-fluid fill" ng-controller = "navigation controller" & gt; & Lt; Div class = "span2 nav-bubble fill" & gt; & Lt; Ul class = "nav nav-list" & gt; & Lt; Li & gt; Option 1 & lt; / Li & gt; & Lt; Li & gt; Option 2 & lt; / Li & gt; & Lt; Li & gt; Option 3 & lt; / Li & gt; & Lt; / Ul & gt; & Lt; / Div & gt; & Lt; Div ng-view & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; / Body & gt;   

and Config:

  Angular Module ("app", ["ngrsource"]). Config ($ route provider) {$ routeProvider when ('/', {redirectTo: '/ dashboard'}). When ('/ dashboard', {controller: 'dashboard controller', template: '/ gpa / app / views /dashboard.html '}) Otherwise ({RedirectTo:' / '});})   

The problem is that when I navigate to / home / index (a hash banged Without) $ location.path () Returns "index /" where it used to move blank before 1.1.15. However, if I expect to go to "/ home / index # dashboard" / dashboard, then I hope that when someone "/" goes "/ dashboard", it was redirection, but navigation The controller is called before being redirected, so it is going to get "/ index".

So how can I tell at least when Hashbang is not included? $ Location.hash () always comes back "". I do not want hard codes to know the "/ index" when there is nothing at the URL.

I think you want to use $ route service and < Hook in the code> $ routeChangeSuccess event.

  App.controller ("Navigation Controller", function ($ radius, $ location ({$ scope. $ ("$ RouteChangeSuccess", function (region, next, current) {$ scope .currentSelection = $ location.path () || "/ dashboard";}); $ scope.select = function (name) {$ scope.currentSelection = name;}});    

No comments:

Post a Comment