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
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
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
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
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 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
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 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
Thank You
data science course in indore
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
analytics
360DigiTMG
Python training in Chennai
typeerror nonetype object is not subscriptable
Best Data Science Courses in Hyderabad
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
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
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.
" Digital Marketing Training Institute in Chennai
Digital Marketing Institute in Bangalore
Digital Marketing Course Near Me"
Tally Course in Chennai
Tally Institute in Chennai
data scientist training and placement in hyderabad
Cloud Computing Training in Chennai
Cloud Training in Chennai
Good post..Thanks for sharing..
Data Science Course in Chennai
Data Science Training in Bangalore
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
Ethical Hacking Course in Chennai
Ethical Hacking course in Bangalore
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
data scientist courses aurangabad
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
Best training n Chennai
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
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
igcse chemistry tutor
ib chemistry tutor
gamsat organic chemistry
cbse organic chemistry
hadoop training in velachery
Big data training in chennai
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 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
Ware House Management Software
We are offerd the SAP HR Training in Delhi
best data science institute in hyderabad