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 B Tech college in Noida
Best B Tech college in Gurgaon
Best B Tech college in UP
Best B Tech college in India
Best B Tech college in Hyderabad
Best B Tech college in Chennai
Best B Tech college in Bangalore
Best B Tech college in Pune
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
python course in pune
python course in chennai
python course in Bangalore
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
Keep update more information..
Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Selenium 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
ccna Training in Chennai
ccna institute in Chennai
Angularjs Training in Chennai
PHP Training in Chennai
Web Designing Course in Chennai
ccna course in chennai
ccna training 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
best software testing training institute in chennai with placement
selenium training
software testing training in chennai
web design training programs
php training center in chennai
magento developer training
AWS Training in bellandur
Marathahalli AWS Training Institues
Kalyan nagar AWS training in institutes
Data Science Training in bellandur
Data Science Training in Kalyan Nagar
Data science training in marathahalli
Data Science Training in Hyderabad
For Data Science training in Bangalore, Visit:
Data Science training in Bangalore
python training in kalyan nagar|python training in marathahalli
selenium training in marathahalli|selenium training in bangalore
devops training in kalyan nagar|devops training in bellandur
phthon training in bangalore
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
IEEE Projects for CSE in Big Data
Spring Framework Corporate TRaining
Final Year Project Centers in Chennai
JavaScript Training 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
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
autocad training institute in Noida
autocad training institute in Delhi
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 basis training in bangalore
sap mm training in bangalore
sap hana training in bangalore
sap fico training in bangalore
sap abap 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
Hadoop Training in Chennai
Big data training in chennai
Big Data Training
bigdata and hadoop training in chennai
Hadoop Training in Velachery
Big data training in Adyar
Python Training in Chennai
Software testing training in chennai
JAVA Training in Chennai
Selenium Training in Chennai
Hadoop Training in Chennai
Big data training in chennai
Big Data Course in Chennai
big data training and placement in chennai
Hadoop Training in Tambaram
Big data training in Guindy
Python Training in Chennai
SEO training in chennai
JAVA Training in Chennai
Selenium Training in Chennai
Spring Training in Chennai
Spring and Hibernate Training
Spring framework Certification
spring training in vadapalani
spring training in Guindy
Hibernate Training in Chennai
javascript training in chennai
QTP Training in Chennai
Mobile Testing Training in Chennai
SAS Training in Chennai
Data science Interview Questions
Data Science Course
Correlation vs Covariance
Simple linear regression
Data Science Institute in Bangalore
Data Science Course in Bangalore
Data Science Training in Bangalore
Thank You
data science course in indore
AWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery
Data Science Certification in Bangalore
data science training in guduvanchery
Data Science In Banglore With Placements
Data Science Course In Bangalore
Data Science Training In Bangalore
Best Data Science Courses In Bangalore
Data Science Institute In Bangalore
Thank you..
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
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
Thank you for sharing such a really admire your post. Your post is great!
data science course in Hyderabad
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
data science
360DigiTMG PMP Certification Course
Software Testing Training in Chennai | Certification | Online
Courses
Software Testing Training in Chennai
Software Testing Online Training in Chennai
Software Testing Courses in Chennai
Software Testing Training in Bangalore
Software Testing Training in Hyderabad
Software Testing Training in Coimbatore
Software Testing Training
Software Testing Online Training
360DigiTMG
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
Cyber Security Course in Bangalore
Cyber Security Training in Bangalore
unindent does not match any outer indentation level
unindent does not match any outer indentation level python
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.
appear on other websites through the Display Network and Google’s AdSense program.
With google ads you can appear in the top for searched keywords.Thus you will receive more relevant customers for your business.
Google Ads Services
You can get Apple-certified repairs and service at the Apple Store or with one of our Apple Authorized Service Providers.
mobile phone repair in North Olmsted
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
RPA Training Institute in Chennai - RPA robots utilize the user interface to capture data and manipulate applications just like humans do. join the Best RPA Training Institute in Chennai now.
Load runner training in Chennai - Load runner Software is an automated performance and testing product from Hewlett- Packard for examining System behavior performance. Join the Best Load Runner Training Institute in Chennai now.
apache Spark training in Chennai - Apache Spark is an open-source distributed general-purpose cluster-computing framework.Join the Best Apache spark Training in Chennai now.
mongodb training in chennai - MongoDB is one of the leading NoSQL databases and it is quite an interesting option in a row of open sources.Join the Best MongoDB Training in Chennai now.
Chennai IT Training Center
machine learning in artificial intelligence
what is reactjs used for
amazon web servers
angularjs development company
aws interview questions and answers for freshers pdf
aws interview questions and answers for devops
Data Science Course in Mumbai
360DigiTMG big data course in malaysia
data science in malaysia
Facebook
Youtube
Tweet
best data science course
Well we really like to visit this site, there is a lot of useful information we can get here.
Data Science Training
data science certification in noida
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
data scientist training and placement
data science training in bangalore
Mobile app development in usa
Android app development company in usa
ios app development in usa
ionic framework development in usa