npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-route-change

v1.0.24

Published

An angular directive which makes it easy to get route data.<br/> There is no need to subscribe to the router, the directive calls a component function with the route data, the function is called upon component initialization and whenever the route changes

Downloads

39

Readme

ng-route-change

An angular directive which makes it easy to get route data. There is no need to subscribe to the router, the directive calls a component function with the route data, the function is called upon component initialization and whenever the route changes.

The route data is an object containing 2 properties:     state: the current route state (url, params, query params)     changes: the changes made to the params and query params from the previous route

For example, when a route user/:id changes from user/12 to user/13 then our component function will be called with this object:

{
    state: {
        url: 'user/13',
        params: {
            id: 13
        }
    },
    changes: {
        params: {
            id: { value: 13, oldValue: 12 }
        }
    }

Placing the directive

Place the directive on any template element or in <ng-container> and pass it an object. Pass your component function to the 'handler' property, leave the second property host:this as is.

<ng-container [NgRouteChange]="{ handler: onRouteChange, host: this }"></ng-container>

The directive will invoke the function with an object of type RouteChangeData, see below for details.

More examples

An example containing params and query params for a route configured as :categoryId/:productId. Navigating from 100/23?name=doll to 170/28?name=brush.

{
    state: {
        url: '170/28?name=brush',
        params: {
            categoryId: 170,
            productId: 28
        },
        queryParams: {
            name: 'brush'
        }
    },
    changes: {
        params: {
            categoryId: { value: 170, oldValue: 100 },
            productId: { value: 23, oldValue: 28 }
        },
        queryParams: {
            name: { value: 'brush', oldValue: 'doll' }
        }
    }
}

note that when the value is all digits it is returned as an integer.

An example of switching between one route to another. Navigating from route configured :categoryId/:productId as 100/23 to route configured user/:id as user/13

{
    state: {
        url: 'user/13',
        params: {
            id: 13
        }
    },
    changes: {
        params: {
            categoryId: { oldValue: 100 },
            productId: { oldValue: 23 },
            id: { value: 13 }
        }
    }
}

RouteChangeData

The component function is called with a single parameter having the interface RouteChangeData:

import { RouteChangeData } from 'ng-route-change';

onRouteChange(data: RouteChangeData) {
    // ...
}

An explanation of RouteChangeData properties:

{
    /*******************************************************************************************************************/
    /*   the 'status' property contains the current values.                                                            */
    /*******************************************************************************************************************/    
    state: {
      url   // the url to navigate to 
      urlAfterRedirects   // if the route config has 'redirectTo', the value is the redirected path
      routeConfigPath   // the route configuration, for example 'todo/:id'
      params   // an objects conatining the params (from the route config) as keys and their values
      queryParams   // an objects conatining the query params (from the url) as keys and their values
    },

    /*******************************************************************************************************************/
    /*   the 'changes' property contains the current values and the previous values.                                   */
    /*   for example, productId: { value: 120, oldValue: 130 }                                                         */
    /*   if the current route does not have :prodctId in its config then the 'value' property will be omitted.         */
    /*   if the previous route did not have :prodctId in its config then the 'oldValue' property will be omitted.      */
    /*******************************************************************************************************************/
    changes: {   
      params   // an objects conatining the params
      queryParams   // an objects conatining the query params
    }
}

How the directive works

The package contains a service which is provided in the root, this service listens to the router events and saves the route data.

The data is shared between all NgRouteChange directives in the app. so for a matter of efficiency, it doesn't matter if the app contains a single directive or multiple directives.

Each directive subscribes to the service on initiation, and unsubscribes on destroy. The service emits the data to the directive whenever a route changes, the directive then calls the component function passing it the data.

In this way the component itself does not need to subscribe/unsubscribe to the router or router events, it receives the data passively with only the data usually needed.

A word of advice

It is recommended to use a meaningful param names on the route config. For example, using user/:userId over user/:id may prevents conflicts in the RouteChangeData 'changes' property where other route configs use :id as well.