Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
angular-example
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
training
angular-example
Commits
fb491b03
Commit
fb491b03
authored
Sep 25, 2018
by
Giorgio de Lodi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
First Commit
parent
8cd7c0d3
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
53 additions
and
5 deletions
+53
-5
app.component.html
src/app/app.component.html
+3
-0
app.component.ts
src/app/app.component.ts
+26
-1
app.module.ts
src/app/app.module.ts
+4
-1
user.model.ts
src/models/user.model.ts
+9
-0
user.service.ts
src/services/user.service.ts
+11
-3
No files found.
src/app/app.component.html
View file @
fb491b03
...
@@ -17,4 +17,7 @@
...
@@ -17,4 +17,7 @@
<h2><a
target=
"_blank"
rel=
"noopener"
href=
"https://blog.angular.io/"
>
Angular blog
</a></h2>
<h2><a
target=
"_blank"
rel=
"noopener"
href=
"https://blog.angular.io/"
>
Angular blog
</a></h2>
</li>
</li>
</ul>
</ul>
<ul>
<li
*
ngFor=
"let u of users"
>
{{u.username}}
</li>
</ul>
src/app/app.component.ts
View file @
fb491b03
import
{
Component
}
from
'@angular/core'
;
import
{
Component
}
from
'@angular/core'
;
import
{
UserService
}
from
'../services/user.service'
;
import
{
User
}
from
'../models/user.model'
;
import
{
Observable
}
from
'rxjs/Observable'
;
@
Component
({
@
Component
({
selector
:
'app-root'
,
selector
:
'app-root'
,
templateUrl
:
'./app.component.html'
,
templateUrl
:
'./app.component.html'
,
...
@@ -7,4 +9,27 @@ import { Component } from '@angular/core';
...
@@ -7,4 +9,27 @@ import { Component } from '@angular/core';
})
})
export
class
AppComponent
{
export
class
AppComponent
{
title
=
'app'
;
title
=
'app'
;
username
:
string
=
"giorgio"
;
password
:
string
=
"giorgio"
;
currentUser
:
User
=
null
;
users
:
Array
<
User
>
;
constructor
(
private
_service
:
UserService
){
this
.
listAll
();
}
doLogin
(){
let
self
=
this
;
this
.
_service
.
login
(
this
.
username
,
this
.
password
).
subscribe
(
data
=>
{
self
.
currentUser
=
data
;
});
}
listAll
(){
let
self
=
this
;
this
.
_service
.
getAll
().
subscribe
((
data
:
any
)
=>
{
self
.
users
=
data
;
},
error
=>
{
console
.
log
(
error
);
});
}
}
}
src/app/app.module.ts
View file @
fb491b03
...
@@ -5,6 +5,9 @@ import { AppComponent } from './app.component';
...
@@ -5,6 +5,9 @@ import { AppComponent } from './app.component';
import
{
HttpClientModule
}
from
'@angular/common/http'
;
import
{
HttpClientModule
}
from
'@angular/common/http'
;
import
{
UserService
}
from
'../services/user.service'
;
@
NgModule
({
@
NgModule
({
declarations
:
[
declarations
:
[
AppComponent
AppComponent
...
@@ -13,7 +16,7 @@ import { HttpClientModule } from '@angular/common/http';
...
@@ -13,7 +16,7 @@ import { HttpClientModule } from '@angular/common/http';
BrowserModule
,
BrowserModule
,
HttpClientModule
HttpClientModule
],
],
providers
:
[],
providers
:
[
UserService
],
bootstrap
:
[
AppComponent
]
bootstrap
:
[
AppComponent
]
})
})
export
class
AppModule
{
}
export
class
AppModule
{
}
src/models/user.model.ts
0 → 100644
View file @
fb491b03
export
class
User
{
public
id
:
number
;
public
name
:
string
;
public
username
:
string
;
public
password
:
string
;
public
email
:
string
;
}
\ No newline at end of file
src/services/user.service.ts
View file @
fb491b03
...
@@ -4,18 +4,26 @@ import {Injectable} from '@angular/core';
...
@@ -4,18 +4,26 @@ import {Injectable} from '@angular/core';
import
{
HttpClient
,
HttpHeaders
}
from
'@angular/common/http'
;
import
{
HttpClient
,
HttpHeaders
}
from
'@angular/common/http'
;
import
{
Observable
}
from
'rxjs/Observable'
;
import
{
Observable
}
from
'rxjs/Observable'
;
import
{
User
}
from
'../models/user.model'
;
const
httpOptions
=
{
const
httpOptions
=
{
headers
:
new
HttpHeaders
({
'Content-Type'
:
'application/json'
})
headers
:
new
HttpHeaders
({
'Content-Type'
:
'application/json'
})
};
};
@
Injectable
()
@
Injectable
()
export
class
UserService
{
export
class
UserService
{
private
url
=
"http://localhost:8080/training"
;
constructor
(
private
http
:
HttpClient
)
{}
constructor
(
private
http
:
HttpClient
)
{}
// Uses http.get() to load data from a single API endpoint
// Uses http.get() to load data from a single API endpoint
getFoods
()
{
getAll
(){
return
this
.
http
.
get
(
'/api/food'
);
return
this
.
http
.
get
<
Array
<
User
>>
(
this
.
url
+
"/users/all"
);
}
login
(
username
:
string
,
password
:
string
){
return
this
.
http
.
post
<
User
>
(
this
.
url
+
"/users/login"
,
JSON
.
stringify
({
username
:
username
,
password
:
password
}),
httpOptions
);
}
add
(
user
:
User
){
return
this
.
http
.
post
<
User
>
(
this
.
url
+
"/users/add"
,
JSON
.
stringify
(
user
),
httpOptions
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment