I'm using the angular-in-memory-web-api module to mock my server response. Here's the in-memory-data.service.ts
import { Injectable } from '@angular/core';
import { InMemoryDbService } from 'angular-in-memory-web-api';
@Injectable()
export class InMemoryData implements InMemoryDbService {
createDb() {
let deviceConfig = [
{
"itemName": "Device Type",
"itemValue": "EtnerNet/IP (Script)",
"editable": false
},
{
"itemName": "Software version",
"itemValue": "V 4.0",
"editable": false
},
{
"itemName": "Script revision",
"itemValue": "37",
"editable": false
},
{
"itemName": "Serial Number",
"itemValue": "12341234",
"editable": true
},
{
"itemName": "Script memory",
"itemValue": "16320",
"editable": true
},
{
"itemName": "Data memory",
"itemValue": "8192",
"editable": true
}
];
let fieldbusConfig: [
{
"itemName": "IP Adress Unitgate",
"itemValue": "0.0.0.0",
"editable": true
},
{
"itemName": "Subnet Mask",
"itemValue": "0.0.0.0",
"editable": true
},
{
"itemName": "IP Adress Gateway",
"itemValue": "0.0.0.0",
"editable": true
},
{
"itemName": "DHCP",
"itemValue": "enabled",
"editable": true
}
];
let applicationProtocol: [
{id: 0, value: "transparent"},
{id: 1, value: "Universal 232"},
{id: 2, value: "Modbus RTU Master"},
{id: 3, value: "Modbus RTU Slave"},
{id: 4, value: "Modus ASCII Master"},
{id: 5, value: "Modbus ASCII Slave"},
{id: 6, value: "3964(R)"},
{id: 7, value: "CS(Pseudo)"},
{id: 8, value: "Universal Modbus RTU Master"},
{id: 9, value: "Universal Modbus RTU Slave"},
{id: 10, value: "Universal ASCII RTU Master"},
{id: 11, value: "Universal ASCII RTU Slave"}
];
return {fieldbusConfig, applicationProtocol, deviceConfig};
}
}
In my config.service.ts I access the collections unsing the URls in the code below
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import {ConfigItem} from './objects/config-item';
@Injectable()
export class ConfigService {
private configUrl ='api/deviceConfig';
private fieldbusConfigUrl ='api/fieldbusConfig';
private headers = new Headers({'Content-Type': 'application/json'});
constructor(private http: Http) { }
getDeviceConfig(): Promise<ConfigItem[]> {
var test;
console.log(this.configUrl);
test = this.http.get(this.configUrl)
.toPromise()
.then(response => response.json().data as ConfigItem[])
.catch(this.handleError);
console.log(test);
return test;
}
getFieldbusConfig(): Promise<ConfigItem[]> {
var test;
console.log(this.fieldbusConfigUrl);
test = this.http.get(this.fieldbusConfigUrl)
.toPromise()
.then(response => response.json().data as ConfigItem[])
.catch(this.handleError);
console.log(test);
return test;
}
// Handle Errors
private handleError(error: any): Promise<any> {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg);
return Promise.reject(errMsg);
}
}
It works fine for the api/deviceConfig URL, da is retrieved and displayed as expected. For the second URL api/fieldbusConfig I get an 404 Error.
Although in an other service called application-data.service.ts
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import {ProtocolItem} from './../objects/protocol-item';
@Injectable()
export class ApplicationDataService {
private applicationConfigUrl ='api/applicationConfig';
private applicationProtocolUrl ='api/applicationProtocol';
private headers = new Headers({'Content-Type': 'application/json'});
constructor(private http: Http){};
getProtocols(): Promise<ProtocolItem[]> {
var test;
test = this.http.get(this.applicationProtocolUrl)
.toPromise()
.then(response => response.json().data as ProtocolItem[])
.catch(this.handleError);
console.log(test);
return test;
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
}
I try to recieve the applicationProtocol collection, this is as well failing with a 404 Error.
I searched through stackoverflow for a solution and is seems to be all right. What is my mistake?
Your syntax is wrong for the second and third lists. You should use "=" instead of ":".
let deviceConfig = [
{
"itemName": "Device Type",
"itemValue": "EtnerNet/IP (Script)",
"editable": false
},
{
"itemName": "Software version",
"itemValue": "V 4.0",
"editable": false
},
{
"itemName": "Script revision",
"itemValue": "37",
"editable": false
},
{
"itemName": "Serial Number",
"itemValue": "12341234",
"editable": true
},
{
"itemName": "Script memory",
"itemValue": "16320",
"editable": true
},
{
"itemName": "Data memory",
"itemValue": "8192",
"editable": true
}
];
let fieldbusConfig = [
{
"itemName": "IP Adress Unitgate",
"itemValue": "0.0.0.0",
"editable": true
},
{
"itemName": "Subnet Mask",
"itemValue": "0.0.0.0",
"editable": true
},
{
"itemName": "IP Adress Gateway",
"itemValue": "0.0.0.0",
"editable": true
},
{
"itemName": "DHCP",
"itemValue": "enabled",
"editable": true
}
];
let applicationProtocol = [
{id: 0, value: "transparent"},
{id: 1, value: "Universal 232"},
{id: 2, value: "Modbus RTU Master"},
{id: 3, value: "Modbus RTU Slave"},
{id: 4, value: "Modus ASCII Master"},
{id: 5, value: "Modbus ASCII Slave"},
{id: 6, value: "3964(R)"},
{id: 7, value: "CS(Pseudo)"},
{id: 8, value: "Universal Modbus RTU Master"},
{id: 9, value: "Universal Modbus RTU Slave"},
{id: 10, value: "Universal ASCII RTU Master"},
{id: 11, value: "Universal ASCII RTU Slave"}
];
return {fieldbusConfig, applicationProtocol, deviceConfig};
}
}
Try this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.