{"id":21177444,"url":"https://github.com/betsol/angular-router","last_synced_at":"2026-01-02T12:04:16.160Z","repository":{"id":16109578,"uuid":"18854653","full_name":"betsol/angular-router","owner":"betsol","description":"Drop-in replacement for default AngularJS router with ability to generate URLs","archived":false,"fork":false,"pushed_at":"2016-01-22T14:42:42.000Z","size":156,"stargazers_count":2,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-20T19:07:37.624Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/betsol.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-16T20:30:41.000Z","updated_at":"2016-01-22T15:00:22.000Z","dependencies_parsed_at":"2022-09-18T07:23:09.952Z","dependency_job_id":null,"html_url":"https://github.com/betsol/angular-router","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/betsol%2Fangular-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/betsol%2Fangular-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/betsol%2Fangular-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/betsol%2Fangular-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/betsol","download_url":"https://codeload.github.com/betsol/angular-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243624735,"owners_count":20321150,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T17:16:08.910Z","updated_at":"2026-01-02T12:04:11.142Z","avatar_url":"https://github.com/betsol.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"This AngularJS module is a drop-in extension for default `ngRoute` module.\n\nWith this module you will be able to automatically generate URLs for named routes,\nthus removing repetitions from your code and making it clear and more robust.\n\n# Why do I need this?\n\nNative `ngRoute` module allows you to define parametrized routes. But it uses this\ndefinitions only to match and parse requests. If you want to build URLs for your routes\nyou have to do this manually.\n\nThe good practice is to use centralized approach, i.e. routing system to both match and\ngenerate URLs. With such system you will be able to define routes only once and reuse this\ndefinitions whenever appropriate.\n\nPlease consider this example:\n\n``` javascript\n$routeProvider\n    .when('/user/:userId/post/:postId/comment/add', {\n        templateUrl: '/partials/user-post-comment-add.html',\n        controller: 'UserPostCommentAddCtrl'\n    })\n;\n```\n\nIn order to dynamically specify this route inside of a view you will have to do something like this:\n\n``` html\n\u003ca href=\"/user/{{ user.id }}/post/{{ post.id }}/comment/add\"\u003eAdd comment\u003c/a\u003e\n```\n\nIn other words, you will have to repeat URL definition every time you need to output it somewhere.\nAnd when time comes to refactor your code and change structure of URLs, you will need to use\nregular expression search to find all places where this URL is specified. What a mess!\n\nYou think there must be a better way? Read on!\n\n# Usage\n\n## Installation\n\n1). Just install `angular-router` where you see fit.\n\nAlso install [`js-router`][js-router] library as `angular-router` is using it internally.\n\nYou can do this with bower: `bower install --save-dev betsol-angular-router`.\n\n2). Include both libraries to your page.\n\n**Example:**\n\n``` html\n\u003cscript type=\"text/javascript\" src=\"betsol/js-router/js-router.js\"\u003e\u003c/script\u003e\n\u003cscript type=\"text/javascript\" src=\"betsol/angular-router/angular-router.js\"\u003e\u003c/script\u003e\n```\n\n3). Add dependency for `ngRouter` in your application's module. Make sure that `ngRoute` is also available.\n\n**Example:**\n\n``` javascript\nangular.module('application', [\n    'ngRoute',\n    'ngRouter',\n    // ...\n]);\n```\n\n4). Just replace all calls to `$routeProvider` with `routerProvider`.\n\n**Example:**\n\n``` javascript\nangularModule\n    .config([\n        function($locationProvider, routerProvider) {\n            $locationProvider.html5Mode(true);\n            routerProvider // Here, \"$routeProvider\" replaced with \"routerProvider\"\n                .when('/user/:userId/post/:postId/comment/add', {\n                    templateUrl: '/partials/user-post-comment-add.html',\n                    controller: 'UserPostCommentAddCtrl'\n                })\n                .when('foo', { /** ... */ })\n                .when('bar', { /** ... */ })\n                .when('baz', { /** ... */ })\n            ;\n        }\n    ])\n;\n```\n\n## Defining named routes\n\nNamed routes are just a normal routes but with a unique name specified. This name is used\nwhenever you need to reference a specific route, e.g. during URL generation.\n\nTherefore we will be calling regular native routes an anonymous routes.\n\nTo convert anonymous route to named route you just need to specify name for it.\nIn order to do so you will need to pass `name` parameter as part of route definition.\n\n**Example:**\n\n``` javascript\nrouterProvider\n    .when('/user/:userId/post/:postId/comment/add', {\n        name: 'user-post-comment-add',\n        templateUrl: '/partials/user-post-comment-add.html',\n        controller: 'UserPostCommentAddCtrl'\n    })\n;\n```\n\nIn this example named route will have a following name: `user-post-comment-add`.\n\n## Generating URLs\n\nIn order to generate URLs for named routes you will need to fetch the `routing` service first.\n\nIn controller you can do it this way:\n\n``` javascript\n.controller('AppCtrl', ['$scope', 'router', function($scope, router) {\n        // \"router\" is an instance of routing service.\n    }\n])\n```\n\nYou can even register routing service at root scope in order to use it inside of any views.\n\n**Example:**\n\n``` javascript\n.controller('AppCtrl', ['$rootScope', 'router', function($rootScope, router) {\n        $rootScope.router = router;\n    }\n])\n```\n\nNow, you are able to use routing service both in your JavaScript world and in the views.\n\n## Using router inside of controller\n\n``` javascript\n.controller('AppCtrl', ['$scope', 'router', function($scope, router) {\n        $scope.url = router.generate('user-post-comment-add', {\n            userId: 100,\n            postId: 500\n        });\n        // $scope.url = '/user/100/post/500/comment/add'\n    }\n])\n```\n\n## Using router in your views\n\n``` html\n\u003ca href=\"{{ router.generate('user-post-comment-add', { userId: 100, postId: 500 }) }}\"\u003eAdd comment\u003c/a\u003e\n\u003c!-- \u003ca href=\"/user/100/post/500/comment/add\"\u003eAdd comment\u003c/a\u003e --\u003e\n```\n\nThat's all! Easy right? ;)\n\n# API description\n\n## `routerProvider` provider\n\nProvider is conforming to native `ngRoute` interface. But adding an optional `name` parameter to route definitions.\n\n## `router` service\n\nRouter service is actually a [`js-router`][js-router] instance with pre-configured named routes.\n\nPlease consult [`js-router`'s][js-router-doc] documentation for it's API description.\n\n# License\n\nThe MIT License (MIT)\n\nCopyright (c) 2014 Slava Fomin II\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n\n[js-router]: https://github.com/betsol/js-router\n[js-router-doc]: https://github.com/betsol/js-router/blob/master/readme.md","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbetsol%2Fangular-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbetsol%2Fangular-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbetsol%2Fangular-router/lists"}