Angular 4 - Modules, components and routing
In the previous post we created a new Angular 4 project. Lets check the how to add modules, components and routing.
You could check out the code from here.
We are going to use bootstrap styling . So lets add bootstrap in our aplication . We are going to use only styling not the components.
We need to add the bootstrap stylesheet in tha .angular-cli.json.
Our first Module
Lets add a new module employee. Inside "src/app" run
Wait we are going to lazy load this module , so we need routing inside the module, right? So we are going to create the routing also.
Time for component
It will create new employee module with its own routing. lets add a component for listing the employee. So inside employee folder run
If you check the employee.module.ts you will find the component in imported and declared in the module, so it will be available for the particular module.
Now we need a service to communicate with API. Lets create that also. So inside employee folder run
We need a modal to save employee. So we create a class employee . So in the same folder run
Routing- lets got the page
So we created all the necessary files . But wait how we are going to load it?
First we will add the routing in the app routing for lazy load the module.
Add these routes inside the const routes .
So now it will redirect to the employee route. But wait where is our list component. We need to add the sub routes and modules in the employee routing file. So in the employee-routing.module.ts change the routes like
Don't forget to import EmployeeListComponent. Now you can see the employee works .
API / Service
Now we need to load data from the API to list the employees. Before that we need to create the data modal for employee. So in employee.ts file add
What we are going to do is, we will have our own services inside each feature module for API calss. Where we will set the API urls and its logic. So in our employee.service.ts we will first inject the angular http service.
lets create a function to get employees from the API.
The http service returns a Observable so we will subscribe to this in the list component. So we are going to inject this EmployeeService inside the list component.
At this time the cli will throw an error that there is provider for this service.
We need to provide this service in employee module. Please read more about service in here.
So add the service to the employee module provider. So in NgModule decorator after declarations add
At this time you migh get another error like
This is because we need to import the map from rxjs .
For knowing more about the ngOnInit read the lifecycle hooks in Angular documentation.
Couple of points need to rememember:
Just like this add a new module and test it. I created a project module that you could try.
You could check out the code from here.
git checkout -b part2.0 origin/part2.0
We are going to use bootstrap styling . So lets add bootstrap in our aplication . We are going to use only styling not the components.
npm install bootstrap@3
We need to add the bootstrap stylesheet in tha .angular-cli.json.
"styles": [ "../node_modules/bootstrap/dist/css/bootstrap.min.css", "styles.css" ],
Our first Module
Lets add a new module employee. Inside "src/app" run
ng generate module employeeor
ng g m employee
Wait we are going to lazy load this module , so we need routing inside the module, right? So we are going to create the routing also.
ng g m employee --routing
Time for component
It will create new employee module with its own routing. lets add a component for listing the employee. So inside employee folder run
ng g c employee-list
If you check the employee.module.ts you will find the component in imported and declared in the module, so it will be available for the particular module.
Now we need a service to communicate with API. Lets create that also. So inside employee folder run
ng g s employee
We need a modal to save employee. So we create a class employee . So in the same folder run
ng g class employee
Routing- lets got the page
So we created all the necessary files . But wait how we are going to load it?
First we will add the routing in the app routing for lazy load the module.
{ path: 'employee', loadChildren: './employee/employee.module#EmployeeModule', },{ path: '**', redirectTo: 'employee', pathMatch: 'full' }
Add these routes inside the const routes .
So now it will redirect to the employee route. But wait where is our list component. We need to add the sub routes and modules in the employee routing file. So in the employee-routing.module.ts change the routes like
const routes: Routes = [{ path: "", children: [{ path: "list", component: EmployeeListComponent }, { path: "", redirectTo: 'list', pathMatch: 'full' }] }];
Don't forget to import EmployeeListComponent. Now you can see the employee works .
API / Service
Now we need to load data from the API to list the employees. Before that we need to create the data modal for employee. So in employee.ts file add
export class Employee { department: string; gender: string; name: string; _id: string; }
constructor(private http: Http) { }
lets create a function to get employees from the API.
getEmployees() { return this.http.get(base_url + "employee").map((data: Response) => { return data.json() }); }
The http service returns a Observable so we will subscribe to this in the list component. So we are going to inject this EmployeeService inside the list component.
constructor(private empService:EmployeeService) { }
At this time the cli will throw an error that there is provider for this service.
ERROR Error: Uncaught (in promise): Error: No provider for EmployeeService!
We need to provide this service in employee module. Please read more about service in here.
So add the service to the employee module provider. So in NgModule decorator after declarations add
providers: [EmployeeService]
At this time you migh get another error like
ERROR TypeError: this.http.get(...).map is not a function
This is because we need to import the map from rxjs .
import 'rxjs/add/operator/map';So that's it now we can call the API and render the result. So in employee-list.component.ts we will call the function.
getEmployees(){ this.empService.getEmployees().subscribe(data=>{ console.log(data); this.employeelist = data; }) } ngOnInit() { this.getEmployees(); }
For knowing more about the ngOnInit read the lifecycle hooks in Angular documentation.
Couple of points need to rememember:
- In employee.service.ts the base_url needs to be your API end point. Later we will set in a single point. For current requirement we just keep as its is.
- Also we have config.ts in the src folder which will have the base_url that you need update the API end point.
- In the map we will convert the response, which in string format, will be converted to JSON.
- Finally you have to import HttpModule from @angular/http in the employee.module.ts for http service to work
- We will have a SharedModule which will not be lazy loaded, and includes the header component. We will add in app.component.html.
Just like this add a new module and test it. I created a project module that you could try.
Comments
Best MCA colleges in noida
TOP MCA colleges in noida
Java interview questions and answers
Core Java interview questions and answers
Java training in Chennai | Java training in Tambaram
Java training in Chennai | Java training in Velachery
Data Science course in kalyan nagar | Data Science Course in Bangalore
Data Science course in OMR | Data Science Course in Chennai
Data Science course in chennai | Best Data Science training in chennai
Data science course in velachery | Data Science course in Chennai
Data science course in jaya nagar | Data Science course in Bangalore
Data Science interview questions and answers
Best Devops training in sholinganallur
Devops training in velachery
Devops training in annanagar
Devops training in tambaram
rpa training in Chennai | rpa training in bangalore | best rpa training in bangalore | rpa course in bangalore | rpa training institute in bangalore | rpa training in bangalore | rpa online training
angularjs interview questions and answers
angularjs Training in bangalore
angularjs Training in bangalore
angularjs interview questions and answers
angularjs Training in marathahalli
angularjs interview questions and answers
microsoft azure training in bangalore
rpa training in bangalore
best rpa training in bangalore
rpa online training
RPA Training in Chennai
Robotics Process Automation Training in Chennai
Blue Prism Training in Chennai
UiPath Training in Chennai
Data Science Course in Chennai
RPA Training in Anna Nagar
RPA Training in Chennai
RPA course in Chennai
Tableau Training in Chennai
Tableau Course in Chennai
Excel Training in Chennai
Corporate Training in Chennai
Pega Training in Chennai
Power BI Training in Chennai
Embedded System Course Chennai
Linux Training in Chennai
Tableau Training in Chennai
Tableau Course in Chennai
English Speaking Classes in Mulund
English Speaking Classes in Mulund West
English Speaking Course in Mulund
English Speaking Course in Mulund East
English Speaking Course in Mulund West
Best English Speaking Classes in Mulund West
English Speaking Classes Mulund
Check out the best
home office desks nz
coffee table nz
bookshelves
asikqq
dewaqq
sumoqq
interqq
pionpoker
bandar ceme terbaik
hobiqq
paito warna terlengkap
syair sgp
web design training programs
php training center in chennai
magento developer training
data sydney
datahk
syair sydney
syairsgp
datasgp
paito warna
http://warungsgp.com/
live hk 6d
live sydney
Web Design Training in Chennai | Web Designing Training and Placement
Web Designing Classes in Chennai | Web Designing Institute in Chennai
mobile application development training course in chennai
data science course in chennai
Digital Marketing training Course in Chennai
digital marketing training institute in Chennai
digital marketing training in Chennai
digital marketing course in Chennai
digital marketing course training in omr
digital marketing certification in omr
digital marketing course training in velachery
digital marketing training center in Chennai
digital marketing courses with placement in Chennai
digital marketing certification in Chennai
digital marketing institute in Chennai
digital marketing certification course in Chennai
digital marketing course training in Chennai
Digital Marketing course in Chennai with placement
digital marketing courses in Chennai
aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore
sap hybris training in bangalore
sap scm training in bangalore
sap pm training in bangalore
sap crm training in bangalore
sap ewm training in bangalore
SAP PP Training in Bangalore
sap s4 hana training in bangalore
sap bw training in bangalore
sap sd training in bangalore
sap hr training in bangalore
sap ehs training in bangalore
sap ps training in bangalore
SAP SRM Training in Bangalore
sap wm training in bangalore
sap security training in bangalore
SAP APO Training in Bangalore
sap gts training in bangalore
sap testing training in bangalore
sap fiori training in bangalore
autocad training institute in Noida
autocad training institute in Delhi
aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore
inplant training for diploma students
mechanical internship in chennai
civil engineering internship in chennai
internship for b.arch students in chennai
internship for ece students in core companies in chennai
internship in chandigarh for ece
industrial training report for computer science engineering on python
internship for automobile engineering students in chennai
big data training in chennai
ethical hacking internship in chennai
paid internships in pune for computer science students
machine learning training in chennai
data science internship in chennai
dot net training in chennai
kaashiv infotech chennai
internship for aeronautical engineering students in india
internship in automobile industry
big data internship in chennai
machine learning internship in chennai
internship in chennai for it students
aws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore
Data Science Course
Data Science Course in Marathahalli
Data Science Course Training in Bangalore
Data Science Course in Chennai
Data Science Courses in Bangalore
Data Science Training in Bangalore
Best Data Science Courses in Bangalore
Data Science Institute in Bangalore
Data Science Training Institutes in Bangalore
Data Science Training in BTM
AWS Training in Bangalore
DevOps Training in Bangalore
PHP Training in Bangalore
Correlation vs Covariance
Simple linear regression
Data Science Course in Bangalore
Data Science Training in Bangalore
Thank You
data science course in indore
Data Science Certification in Bangalore
Web Designing Training in Chennai
Web Designing Course in Chennai
Web Designing Training in Bangalore
Web Designing Course in Bangalore
Web Designing Training in Hyderabad
Web Designing Course in Hyderabad
Web Designing Training in Coimbatore
Web Designing Training
Web Designing Online Training
Correlation vs Covariance
Simple linear regression
data science interview questions
analytics
Cloud Computing Projects
Networking Projects
Final Year Projects for CSE
JavaScript Training in Chennai
JavaScript Training in Chennai
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
360DigiTMG
360DigiTMG Data Analytics Course
python training in bangalore | python online Training
artificial intelligence training in bangalore | artificial intelligence online training
machine learning training in bangalore | machine learning online training
uipath-training-in-bangalore | uipath online training
blockchain training in bangalore | blockchain online training
aws training in Bangalore | aws online training
data science training in bangalore | data science online training
Simple Linear Regression
Correlation vs covariance
data science interview questions
KNN Algorithm
Logistic Regression explained
Python training in Chennai
typeerror nonetype object is not subscriptable
Data Science training in Mumbai
Data Science course in Mumbai
SAP training in Mumbai
Best Data Science Courses in Hyderabad
unindent does not match any outer indentation level
DevOps training in chennai - Best DevOps training Institute and training in Chennai will help and make you to become a DevOps programmer.
blue prism training in Chennai - Best Blue prism training and technology in Chennai always provide better and complete guidance on the Trending technologies such as Blue prism and various other technology.
microsoft azure training in chennai - Best Microsoft azure training in Chennai offering course and guidance on the Microsoft Azure technology.
vé máy bay đi cao hùng
lich ban ve may bay tet 2021
vé máy bay giá rẻ khứ hồi đi đà nẵng
vé máy bay đi nha trang tháng 5
vé máy bay tphcm đi đà lạt vietnam airlines
Best Institutes For Digital Marketing in Hyderabad
data science in malaysia
Facebook
Youtube
Tweet
Well we really like to visit this site, there is a lot of useful information we can get here.
Data Science Training
vé máy bay đi Mỹ bao nhiêu
vé máy bay từ mỹ về việt nam hãng ana
gia ve may bay ha noi da nang
đặt vé máy bay đi hà nội
giá vé máy bay tphcm đi nha trang
data science training in bangalore
Chennai IT Training Center
R programming training in chennai - The R programming course would help to multiply your knowledge in statistical and graphical counting. R includes many other series machine learning algorithms, linear regression & statistical inference.
blueprism training in chennai - Robotic Process Automation (RPA) software developed by Blue prism group. Actually Blue pro=ism is a latest technology and a leading tool for RPA. It is software which reduces human error and can icrease the efficency of organisation.
AWS Training Institute in Chennai - Amazon Web Services is one of the most commonly used cloud platform that presents you with all the features that required by any well-grown company at affordable price segement.
data science in bangalore
" Digital Marketing Training Institute in Chennai
Digital Marketing Institute in Bangalore
Digital Marketing Course Near Me"
Devops Training in Hyderabad
Hadoop Training in Hyderabad
Python Training in Hyderabad
Tableau Training in Hyderabad
Selenium Training in Hyderabad
Tally Course in Chennai
Tally Institute in Chennai
data scientist training and placement in hyderabad
Cloud Computing Training in Chennai
Cloud Training in Chennai
swift training in bangalore
swift developer training in chennai
Good post..Thanks for sharing..
Data Science Course in Chennai
Data Science Training in Bangalore
data science training in chennai
DOT NET Training in Chennai
DOT NET Course in Chennai
Digital Marketing Course in Chennai
Best digital marketing course online
Digital Marketing Courses in Bangalore
AWS Training in Chennai
AWS Training in Bangalore
Ethical Hacking Course in Chennai
Ethical Hacking course in Bangalore
Data Science Training in Chennai
Data Science Courses in Bangalore
Machine Learning Training in Chennai
PHP Training in Chennai
RPA Training in Bangalore
RPA course in Bangalore
RPA Training in Chennai
RPA Training Institute in Bangalore
Robotic Process Automation Training in Chennai
RPA Classes in Chennai
Digital Marketing Training Institute in Chennai
Digital Marketing Course Near Me
Digital Marketing Institute in Bangalore
adidas originals panna scarpe uomo basse
grossista condizionatori
costumi interi amazon
vestito da elsa disney
adidas yeezy semi frozen yellow on feet
second hand golf 5 gti for sale
piumini daunenstep amazon
pittura x pareti amazon trascorrere
fiore garofano amazon
air force 1 nike outfit men basket
nike air zip up hoodie
king kong jeans
zapatillas running galaxy 4 azul marino mujer adidas
blazer basicos mujer
nike air max de bebe principal opción
zapatos de charol mujer con taco
nike metcon flyknit 3 para running
bolsas para comprimir equipaje
chanclas new balance niña
nike pegasus grey and green
amazon sandalias reef
polo simulator
data scientist training and placement in hyderabad
data scientist courses aurangabad
nike zoom strike sprinter
gants si assault factory pilot noir oakley
camisa cuadros franela mujer
galocha rosa feminina
adidas run falcon on feet
ghete galbene piele
hama stiftplatte einhorn amazon
moleton nike preço
veste velour carhartt
bañador neopreno mujer oysho
jogging fille 14 ans adidas
melhores marcas de mochilas masculinas
beistelltisch mit stauraum
nike air max 97 silber
adidas niño verde agua
béret a pompon bretelles uniforme
rompecabezas de obras de arte para arma
venta de bicicletas para ejercicio
sudadera nike vintage
lidl online dirndl
adidas noir doré
foot locker nike react element
filtre pour aspirateur samsung sc4780
adidas stabil x junior
Great post.Thanks for sharing such a worthy information...
SEO Training in Bangalore
SEO Course in Bangalore
SEO Training Institute in Bangalore
Best SEO Training Institute in Bangalore
SEO Training Bangalore
business analytics courses
Best training n Chennai
Data Science Course in Faridabad
Python for Data Science
Learn Python for Data Science
More impressive Blog!!! Its more useful for us...Thanks for sharing with us...
Why is Big Data Important?
Why Big Data
Grab Oracle DBA Training in Chennai | Infycle Technologies
Description:
Set your career goal towards Oracle for a wealthy future with Infycle. Infycle Technologies is the best software training institute in Chennai, which gives the most trusted and best Oracle DBA Training in Chennai with hands-on practical training that will be guided by professional tutors in the field. In addition to this, the mock interviews will be given to the candidates, so that they can face the interviews with full confidence. Apart from all, the candidates will be placed in the top MNC's with a great salary package. To get it all, call 7502633633 and make this happen for your happy life.
best training institute in chennai
AWS Certification in Chennai
AWS course
Best AWS Training in Bangalore
best java institute in chennai
best java training institute in chennai
Mobile App Development Courses
Mobile App Development Courses in Chennai
Mobile App Development Courses in Bangalore
Data Science Requirements
Career In Data Science
infertility in men
sperm motility
fertility center in chennai
loadrunner online training
Loadrunner Course in Chennai
LoadRunner Training in Bangalore
Swift Online Course
swift developer training in chennai
swift developer training in Bangalore
Google Analytics Training In Chennai
Google Analytics Online Course
Ionic Online Course
Ionic Training in Chennai
Ionic Training in Bangalore
igcse chemistry tutor
ib chemistry tutor
gamsat organic chemistry
cbse organic chemistry
hadoop training in velachery
Big data training in chennai
Ionic Course in Chennai
Ionic Online Training
Ionic Course in Bangalore
Angularjs training in anna nagar
Angularjs Training in Chennai
data analytics course in hyderabad
This post is so interactive and informative.keep update more information…
Artificial Intelligence Course in Velachery
Artificial Intelligence Course in Chennai
data science course delhi
data analytics training in aurangabad
Top Tips to become a successful swift programmer
How become a successful swift programmer
Swift Developer Course in Kochi
Swift Developer Course in Trivandrum
Swift Developer Course in Ahmedabad
business analytics course in bhubaneswar
Ware House Management Software
We are offerd the SAP HR Training in Delhi
best data science institute in hyderabad
AngularJS Development Services