AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) _ Adrian Mejia Blog
AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) _ Adrian Mejia Blog
This tutorial is meant to be as clear as possible. At the same time, we are going to cover
the concepts that you will need most of the time. All the good stuff without the fat :)
“ Check out the updated version of this post in here: Create a CRUD App with Angular 9+
and TypeScript
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 1/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
3. MEAN Stack Tutorial: MongoDB, ExpressJS, AngularJS and NodeJS (Part III)
We are going to start building all the examples in a single HTML file! It embedded
JavaScript and NO styles/CSS for simplicity. Don’t worry, in the next tutorials, we will
learn how to split use Angular modules. We are going to break down the code, add
testing to it and styles.
What is Angular.js?
Angular.js is a MVW (Model-View-Whatever) open-source JavaScript web framework
that facilitates the creation of single-page applications (SPA) and data-driven apps.
AngularJS motto is
It extends standard HTML tags and properties to bind events and data into it using
JavaScript. It has a different approach to other libraries. jQuery, Backbone.Js, Ember.js
and similar… they are more leaned towards “Unobtrusive JavaScript”.
Traditional JavaScript frameworks, use IDs and classes in the elements. That gives the
advantage of separating structure (HTML) from behavior (Javascript). Yet, it does not do
any better on code complexity and readability. Angular instead declares the event
handlers right in the element that they act upon.
Times have changed since then. Let’s examine how AngularJS tries to ease code
complexity and readability:
Unit testing ready: JavaScript is, usually, hard to unit test. When you have DOM
manipulations and business logic together (e.g. jQuery based code). AngularJS
keeps DOM manipulation in the HTML and business logic separated. Data and
dependencies are $injected as needed.
DOM manipulation where they are used. It decouples DOM manipulation from
application logic.
AngularJS is also excellent for single-page applications (SPA).
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 2/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
AngularJS Directives
The first concept you need to know about AngularJS is what are directives.
Directives are extensions of HTML markups. They could take the form of attributes,
element names, CSS class and or even HTML comments. When the AngularJS
framework is loaded, everything inside ng-app it’s compiled. The directives are bound
to data, events, and DOM transformations.
Notice in the following example that there are two directives: ng-app and ng-model.
Notice in the following example that there are two directives: ng-app and ng-model .
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 3/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
1 <html ng-app>
2 <head>
3 <title>Hello World in AngularJS</title>
4 </head>
5 <body>
6
7 <input ng-model="name"> Hello {{ name }}
8
9 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angu
10 </body>
11 </html>
ng-model: is a directive that binds usually form elements. For instance, input ,
select , checkboxes , textarea . They keep data (model) and visual elements
(HTML) in sync.
EDIT ON
HTML Result
You can create your own directives. Checkout the this tutorial for more: creating-custom-
angularjs-directives-for-beginners. It will go deeper into directives.
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 4/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
Whenever the HTML is changed, the model gets updated. Wherever the model gets
updated it is reflected in HTML.
AngularJS Scope
$scope it is an object that contains all the data to which HTML is bound. They are the
glue your javascript code (controllers) and the view (HTML). Everything that is attached
to the $scope , it is $watch ed by AngularJS and updated.
Scopes can be bound to javascript functions. Also, you could have more than one
$scope and inherit from outer ones. More on this, in the controller’s section.
AngularJS Controllers
Angular.js controllers are code that “controls” certain sections containing DOM
elements. They encapsulate the behavior, callbacks and glue $scope models with
views. Let’s see an example to drive the concept home:
1 <body ng-controller="TodoController">
2 <ul>
3 <li ng-repeat="todo in todos">
4 <input type="checkbox" ng-model="todo.completed">
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 5/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
EDIT ON
HTML Result
As you might notice we have new friends: ng-controller , ng-repeat and $scope .
ng-controller is a directive that tells angular what function controller to use for a
particular view. Every time AngularJS loads, it reads the ng-controller argument
(in this case “TodoController”). Then, it will look for a function in plain old javascript
object (POJO) with the same name or for angular.controller matching name.
$scope As mentioned earlier $scope ‘s are the glue between the data models in
the controllers and the views. Take a look to our “TodoController” it has a parameter
named $scope . AngularJS is going to pass ( $inject ) that parameter, and
whatever you attach to it, it will be available in the view. In this example is the
particular is the todos array of objects.
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 6/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
ng-repeat as its name implies, it is going to “repeat” the element and sub-
elements where this directive is declared. It is going to iterate for each element in
the $scope.todos array.
AngularJS Modules
Modules are a way to encapsulate different parts of your application. They allow reusing
code in other places. Here’s an example of how to rewrite our controller using modules.
1 angular.module('app', [])
2 .controller('TodoController', ['$scope', function ($scope) {
3 $scope.todos = [
4 { title: 'Learn Javascript', completed: true },
5 { title: 'Learn Angular.js', completed: false },
6 { title: 'Love this tutorial', completed: true },
7 { title: 'Learn Javascript design patterns', completed: false },
8 { title: 'Build Node.js backend', completed: false },
9 ];
10 }]);
EDIT ON
HTML JS Result
Using modules brings many advantages. They can be loaded in any order, and parallel
dependency loading. Also, tests can only load the required modules and keep it fast,
clear view of the dependencies.
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 7/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
AngularJS Templates
Templates contain HTML and Angular elements (directives, markup, filters or form
controls). They can be cached and referenced by an id.
Here’s an example:
Notice they are inside the script and has a type of text/ng-template .
It does not come with AngularJS core module, so we have to list it as a dependency. We
are going to get it from Google CDN:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-
route.min.js"></script>
NEW FEATURE: add notes to the todo tasks. Let’s start with the routes!
1 angular.module('app', ['ngRoute'])
2 .config(['$routeProvider', function ($routeProvider) {
3 $routeProvider
4 .when('/', {
5 templateUrl: '/todos.html',
6 controller: 'TodoController'
7 });
8 }]);
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 8/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
EDIT ON
HTML JS Result
ngView is a directive used by $routeProvider to render HTML into it. Every time
the URL changes, it will inject a new HTML template and controller into ngView.
1 angular.module('app', ['ngRoute'])
2
3 .factory('Todos', function(){
4 return [
5 { name: 'AngularJS Directives', completed: true },
6 { name: 'Data binding', completed: true },
7 { name: '$scope', completed: true },
8 { name: 'Controllers and Modules', completed: true },
9 { name: 'Templates and routes', completed: true },
10 { name: 'Filters and Services', completed: false },
11 { name: 'Get started with Node/ExpressJS', completed: false },
12 { name: 'Setup MongoDB database', completed: false },
13 { name: 'Be awesome!', completed: false },
14 ];
15 })
16
17 .controller('TodoController', ['$scope', 'Todos', function ($scope, To
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 9/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
18 $scope.todos = Todos;
19 }])
We are now injecting the data dependency Todo into the controllers. This way we could
reuse the data to any controller or module that we need to. This is not only used for
static data like the array. But we could also do server calls using $http or even
RESTful $resource .
Let’s say we want to show the details of the task when we click on it. For that, we need
to create a 2nd controller, template, and route that uses this service:
(NOTE: Click on the links and it will take you to the todo details. Use backspace key to
go back to the main menu)
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 10/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
NOTE: in codepen, you will not see the URL. If you want to see it changing, you can
download the whole example an open it from here.
AngularJS Filters
Filters allow you to format and transform data. They change the output of expressions
inside the curly braces. AngularJS comes with a bunch of useful filters.
Built-in Filters:
Note you can also chain many filters and also define your own filters.
“ NEW FEATURE: Search todo tasks by name. Let’s use a filter to solve that problem.
Notice that we are using search.name in the ng-model for search. That will limit the
search to the name attribute and search.notes will look inside the notes only. Guest
what search would do then? Precisely! It searches in all the attributes. Fork the
following example and try it out:
What’s next?
Congrats! You have completed part 1 of this 3 part series. We are going to build upon
the things learned in here, in the next post we are going to setup a backend in NodeJS
and MongoDB and connect it to AngularJS to provide a full featured CRUD app.
Continue with:
BackboneJS Tutorials
ng-test
Congrats, you have reached this far! It is time to test what you have learned. Test-Driven
Learning (TDL) ;). Here’s the challenge: open this file on your favorite code editor. Copy
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 12/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
the boilerplate code and built the full app that we just build in the previous examples. Of
course, you can take a peek from time to time if you get stuck ;)
index.html
-OR-
ng-solution
This is the full solution and you can see it live in here.
1 <html ng-app="app">
2 <head>
3 <title>ngTodo</title>
4 </head>
5 <body>
6
7 <ng-view></ng-view>
8
9 <!-- Libraries -->
10 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
11 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.min.js"></script>
12
13 <!-- Template -->
14 <script type="text/ng-template" id="/todos.html">
15 Search: <input type="text" ng-model="search.name">
16 <ul>
17 <li ng-repeat="todo in todos | filter: search">
18 <input type="checkbox" ng-model="todo.completed">
19 <a href="#/{{$index}}">{{todo.name}}</a>
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 13/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
20 </li>
21 </ul>
22 </script>
23
24 <script type="text/ng-template" id="/todoDetails.html">
25 <h1>{{ todo.name }}</h1>
26 completed: <input type="checkbox" ng-model="todo.completed">
27 note: <textarea>{{ todo.note }}</textarea>
28 </script>
29
30 <script>
31 angular.module('app', ['ngRoute'])
32
33 //---------------
34 // Services
35 //---------------
36
37 .factory('Todos', function(){
38 return [
39 { name: 'AngularJS Directives', completed: true, note: 'add notes...' },
40 { name: 'Data binding', completed: true, note: 'add notes...' },
41 { name: '$scope', completed: true, note: 'add notes...' },
42 { name: 'Controllers and Modules', completed: true, note: 'add notes...' },
43 { name: 'Templates and routes', completed: true, note: 'add notes...' },
44 { name: 'Filters and Services', completed: false, note: 'add notes...' },
45 { name: 'Get started with Node/ExpressJS', completed: false, note: 'add notes...' },
46 { name: 'Setup MongoDB database', completed: false, note: 'add notes...' },
47 { name: 'Be awesome!', completed: false, note: 'add notes...' },
48 ];
49 })
50
51 //---------------
52 // Controllers
53 //---------------
54
55 .controller('TodoController', ['$scope', 'Todos', function ($scope, Todos) {
56 $scope.todos = Todos;
57 }])
58
59 .controller('TodoDetailCtrl', ['$scope', '$routeParams', 'Todos', function ($scope, $routeParams, To
60 $scope.todo = Todos[$routeParams.id];
61 }])
62
63 //---------------
64 // Routes
65 //---------------
66
67 .config(['$routeProvider', function ($routeProvider) {
68 $routeProvider
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 14/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
69 .when('/', {
70 templateUrl: '/todos.html',
71 controller: 'TodoController'
72 })
73
74 .when('/:id', {
75 templateUrl: '/todoDetails.html',
76 controller: 'TodoDetailCtrl'
77 });
78 }]);
79 </script>
80
81 </body>
82 </html>
NEWER OLDER
Creating RESTful APIs with NodeJS and MongoDB Cheap Airplay receiver with
Tutorial (Part II) Raspberry Pi
email address
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 15/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
Subscribe
Follow @iAmAdrianMejia
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 16/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
Contents
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 17/18
3/17/25, 2:17 PM AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I) | Adrian Mejia Blog
1. What is Angular.js?
2. AngularJS Main Components
3. What’s next?
45 Security Tips to Building a Node.js static Self-balanced Binary Tree Data Str
Avoid Hacking file server … Search Trees with … JavaScript fo
6 years ago • 2 comments 6 years ago • 5 comments 6 years ago • 2 comments 6 years ago • 9 c
JavaScript tutorials and web JavaScript tutorials and web JavaScript tutorials and web JavaScript tuto
development articles development articles development articles development a
including topics like … including topics like … including topics like … including topics
66 Comments
1 Login
Name
Aadi Nickels − ⚑
10 years ago
Thank you, this is the most practical tutorial I have found that incorporates the full mean stack step by
step without asking you to incorporate scaffolding like MEAN.js - THANK YOU! I have been specifically
having issues with routing and just this first third helped a bunch!
74 0 Reply ⥅
https://adrianmejia.com/angularjs-tutorial-for-beginners-with-nodejs-expressjs-and-mongodb/ 18/18