Angular simple patternless routing

I am looking for a simple solution with Angular how to handle routes.

I have a page generated by a server with a simple google map and a little logic, which is located in 3 separate controllers.

Now I want to connect routing inside this. Just simple routing. When I move with the map and get the new coordinates, I want to insert them into the current url as param "? Position = 10.11.50.23". I want to use push state history with hashbang backup.

In another part of the application, I want to listen about a change in this parameter (can some $ watch be?). And I want to use the same code to detect the changes that will be used when the page loads.

I used CanJS in a previous project, where it was absolutely simple, but in Angular I cannot take a step to fix the result :)

PS: it's just a reduction in the minimum amount.

thanks

+4
source share
2 answers

You do not need angular routing for this. Routing in angular is usually used to replace ng-viewwith various patterns, although it can be used for other things .

Your requirements can be met through the service $locationand settings $watch:

$scope.$watch(function () { return $location.search().position; }, function (newVal) {
  $scope.params = newVal || 'No params';
});

$scope.setParams = function (val) {
  $location.search('position', val);
};

Working demo

Run live-preview in a separate window to see the change in the parameters in the address bar of the window.

: plnkr.co. , , .

+4

CanJS, angular .

: $location.search({parameter1:'someValue', parameter2: 'someOtherValue'}) URL- - .

URL- ( ) : $locationChangeStart $locationChangeSuccess, :

    $scope.$on("$locationChangeStart", function (event, next, current) {
        // your staff goes here
    });
    $scope.$on("$locationChangeSuccess", function (event, next, current) {
        // or here (or both if needed)
    });

, $location.search() .

+2

All Articles