JarodMS

Mobile App Development with Ionic, Angular, and More!

Menu
  • Home
  • Mobile Apps
  • About Me
  • Contact Me
  • Diary
Menu

Angular app using Node, JSON server, NG-Bootstrap, and NgRx

Posted on March 5, 2020April 22, 2020 by Jarod

Source
https://github.com/jarodms/angular-nodejs-bootstrap-ngrx
[UPDATE 04/09/2020 – The master branch was updated to use the createAction factory; the branch action-class uses classes for creating actions.]

Resources
https://angular.io/
https://ngrx.io/docs

Background

I had previously built a standalone app that featured a “JSON Server”, NodeJS, and Angular front-end that would store your “ToDo” items in a local db.json file. This was a rather simple example of using API’s in an Angular App without the real need to have a full-fledged backend server. With one simple command you could run everything to try it out:

npm run start-dev

The source for this basic app: https://github.com/jarodms/angular-nodejs-bootstrap

This was nice and simple, but I wanted to use some of the things that I had recently learned with NgRx and apply it to this app. So I built a new version that is pretty much the same except that it uses NgRx: https://github.com/jarodms/angular-nodejs-bootstrap-ngrx

NgRx

This framework had been taboo to me for a while. I had used it a while back and never really took the time to understand it as much as I should have. But recently I took it upon myself to “better understand rather than just use”. That’s what we programmers do right? Learn, implement, learn some more.

In the world of NgRx and state management, a very light and simple “ToDo” app might not be the best place to use NgRx. NgRx has always seemed to be for larger apps. But that is the reason I wanted to build this example app, because it is so simple and can make for understanding NgRx even easier. And…you never know when your simple app will become much, much larger.

Commands

Here are some basic commands I used throughout building this app. Note that some are dev dependencies and some are not.

npm install @ngrx/schematics --save-dev

Scaffolding library for Angular applications. It helps us generate files using the Angular CLI.


npm install @ngrx/store-devtools --save-dev

Dev Tools for debugging NgRx apps. You will still need an extension for viewing the data in your browser. Here’s one for Chrome: https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd
 and one for Firefox: https://addons.mozilla.org/en-US/firefox/addon/reduxdevtools/


npm install @ngrx/store
npm install @ngrx/effects

* other dependencies


Steps

  1. Setup the Angular app to use NgRx
    ng generate @ngrx/schematics:store State --root --module app.module.ts
  2. Add a Reducer
    ng g @ngrx/schematics:reducer Todos --group
  3. Add an Action
    ng g @ngrx/schematics:action Todo --group
  4. Add an Effect
    ng g @ngrx/schematics:effect Todo --group --root

For more info on the above commands, see the NgRx documentation: https://ngrx.io/guide/schematics

Result

Once all the code was added, you can see the result of what we need to do for the main ToDo component of todos.component.ts located in src/app/todos/todos.component.ts

Notice that I am no longer injecting TodosService in the constructor as I was in the non-NgRx example. The constructor is subscribing to the store for changes. When I need to add, delete, or complete a Todo, I am not calling a service directly. Rather, I am dispatching an Action. The side-effect (Effect) of dispatching these actions will be to update the state, and therefore my component will receive updated data.

Larger App

So with this example, I was able to immediately apply it to what will soon be a larger mobile app using Angular and Ionic.

The Scenario: What if you needed to get the location of the user, and based on that location retrieve data using an API that will show location-based content to the user. 

In the past, before NgRx, I would do something like this:

this.geolocation
     .getCurrentPosition().then(resp => {

       this.location = resp.coords;

       // Call the web service that uses the location to get content
this.updateShows();

// Now update my stats server

// Now call this API to do something else

// Do something else too....
     })
     .catch(error => {
      });
 
There are various ways to do this. I could place location info in a service and then have another service that depends on the location service that gets the data. The point is, I need to wait for my location before retrieving the data. 
 
Now, with NgRx, in my LocationService I do something like this:
 
this.geolocation.getCurrentPosition()
     .then(resp => {

       this.location = resp.coords;
        this.store.dispatch(new UpdateLocation());

     })
     .catch(error => {
      });
My component:
constructor(private store: Store<State>) {
store.select(state => state.shows).subscribe(shows => this.allShows = shows.shows); }
When I have the location, I dispatch an Action of UpdateLocation. The side-effect (Effect) will then do what it needs to do with an updated location. Right now it dispatches another action to LoadEvents. This action will eventually call the EventsService API to get a list of events.
 
I could also do X, Y, and Z in my Effect, and my LocationService and EventsService don’t really care or need to know. I could log a notice to my cloud data. Call another service that tracks stats for users in the area. Whatever. The point is that it slims down my services and decouples my components from services. Everything is Action and Effect based for the most part. I love it. Love it!
 
Please follow:
error
Tweet
1+

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Newsletter

Signup with your email address to receive news and updates.

Search

Recent Posts

  • Not able to call REST API on Android, but it works with iOS and Web App
  • Ionic – the platform was not ready when I was trying to get the location
  • My Dividend Meter
  • Angular app using Node, JSON server, NG-Bootstrap, and NgRx
  • LoadingController example showing “Loading…” message
  • Adding the Version Number in your app
  • PhoneGap Build – Config File
  • Example Angular 8 with NgRx and Redux DevTools
  • Update an Ionic4/Angular7 app to use Angular 8
  • Ionic 4 and Angular with PouchDB

Need Help?

I’m available for Ionic and Angular contract work. Contact me to get started.

My Links

All my apps on Google Play Store
All my apps on Apple App Store
GitHub
LinkedIn

Archives

  • October 2020
  • April 2020
  • March 2020
  • November 2019
  • October 2019
  • August 2019
  • November 2018
  • August 2018
  • July 2018

Categories

  • Angular
  • Beginner
  • Ionic
  • Ionic Component
  • PhoneGap Build
  • Uncategorized
  • Visual Notification

Available for Angular and Ionic freelance work including custom app development.

Tags

Angular AuthGuard component Custom Icon Firebase Font Awesome Geolocation Google Cloud Functions icons ionic3 Ionic4 iOS NgRx PhoneGap Build tabs VirtualScroll
© 2025 JarodMS | WordPress Theme by Superbthemes