Main component code
====================
@Component({
selector: 'name',
template: require('./name.template.html'),
providers: [NameService],
})
export class NameComponent implements OnInit {
constructor(private _ns: NameService) {
-------------
-------------
}
ngOnInit() {
----------
----------
}
saveNameDetails(obj) {
let dataList = {};
dataList['name'] = obj['name'];
_ns.save(dataList);
}
}
service component code:
========================
import { Response, Http } from '@angular/http';
--------------------
--------------------
@Injectable()
export class NameService {
constructor(private _http: Http) { }
save(body) : Observable<any> {
return this._http.put(URL, body, this.headerOptions)
.map(this.extractData)
.map(this.doAction)
.catch(this.handleErrorMessage);
}
private extractData(res: Response) {
return res;
}
private doAction(response: any) {
return response;
}
private handleErrorMessage(errorObj: Response | any) {
let _body = JSON.parse(errorObj['_body']) || {};
let errorMessage: string;
switch (errorObj.status) {
case 400:
case 401:
errorMessage = _body['error'];
break;
case 404:
break;
}
return Observable.throw(errorMessage);
}
---------------------
---------------------
}
====================
@Component({
selector: 'name',
template: require('./name.template.html'),
providers: [NameService],
})
export class NameComponent implements OnInit {
constructor(private _ns: NameService) {
-------------
-------------
}
ngOnInit() {
----------
----------
}
saveNameDetails(obj) {
let dataList = {};
dataList['name'] = obj['name'];
_ns.save(dataList);
}
}
service component code:
========================
import { Response, Http } from '@angular/http';
--------------------
--------------------
@Injectable()
export class NameService {
constructor(private _http: Http) { }
save(body) : Observable<any> {
return this._http.put(URL, body, this.headerOptions)
.map(this.extractData)
.map(this.doAction)
.catch(this.handleErrorMessage);
}
private extractData(res: Response) {
return res;
}
private doAction(response: any) {
return response;
}
private handleErrorMessage(errorObj: Response | any) {
let _body = JSON.parse(errorObj['_body']) || {};
let errorMessage: string;
switch (errorObj.status) {
case 400:
case 401:
errorMessage = _body['error'];
break;
case 404:
break;
}
return Observable.throw(errorMessage);
}
---------------------
---------------------
}