Angular : How to get constant values in the component after setting them?
I've a constant array of orders like this in constants.ts file
export const ORDERS = [
'id': 'PROCESSING',
'displayName': null
,
'id': 'SHIPPED',
'displayName': null
];
Here in my component I'm setting the value of 'displayName'
import ORDERS from './constants';
-----------------
--------------
setOrders()
ORDERS.forEach((order: any) =>
if(order.id === 'PROCESSING')
order.displayName = 'Processing';
if(order.id === 'SHIPPED')
order.displayName = 'Shipped';
);
But here when I try to get values of 'displayName' in another funtion it return null, but in the object order I can see the value of 'displayName'
getOrders()
ORDERS.forEach((order: any) =>
console.log(order); // displayName exists in the object
console.log(order.displayName); // But here null
);
Both functions are in a same component. Please help me how to resolve this thanks.
Update : Full implementation
getTimePeriod()
return Promise.resolve((() =>
this.orderService.getTimePeriod()
.subscribe(
(data) =>
this.orderStatusList = data.configParameters;
this.orderStatusList.forEach(
(s: any) =>
if (s.status === 'PROCESSING') this.setOrders(s.status, s.name);
if (s.status === 'SHIPPED') this.setOrders((s.status, s.name);
);
,
error =>
this.errorMessage = this.orderService.handleError(error);
);
)());
setOrders(status, name)
ORDERS.forEach((order: any) =>
if (order.id === status)
order.displayName = name;
);
getOrders()
return Promise.resolve((() =>
ORDERS.forEach((order: any) =>
const displayName = order;
console.log(order); // displayName exists in order
console.log(displayName); // Null
this.orderService.getOrdersByDisplayName(buildQuery(null, displayName, null, null))
.subscribe(
(data) =>
this.ordersWidgetInfo.push(
...order,
...data,
number: data.orders && data.orders.length,
);
,
error =>
console.log(`error for $orderType : $error`);
);
);
)());
angular typescript
add a comment |
I've a constant array of orders like this in constants.ts file
export const ORDERS = [
'id': 'PROCESSING',
'displayName': null
,
'id': 'SHIPPED',
'displayName': null
];
Here in my component I'm setting the value of 'displayName'
import ORDERS from './constants';
-----------------
--------------
setOrders()
ORDERS.forEach((order: any) =>
if(order.id === 'PROCESSING')
order.displayName = 'Processing';
if(order.id === 'SHIPPED')
order.displayName = 'Shipped';
);
But here when I try to get values of 'displayName' in another funtion it return null, but in the object order I can see the value of 'displayName'
getOrders()
ORDERS.forEach((order: any) =>
console.log(order); // displayName exists in the object
console.log(order.displayName); // But here null
);
Both functions are in a same component. Please help me how to resolve this thanks.
Update : Full implementation
getTimePeriod()
return Promise.resolve((() =>
this.orderService.getTimePeriod()
.subscribe(
(data) =>
this.orderStatusList = data.configParameters;
this.orderStatusList.forEach(
(s: any) =>
if (s.status === 'PROCESSING') this.setOrders(s.status, s.name);
if (s.status === 'SHIPPED') this.setOrders((s.status, s.name);
);
,
error =>
this.errorMessage = this.orderService.handleError(error);
);
)());
setOrders(status, name)
ORDERS.forEach((order: any) =>
if (order.id === status)
order.displayName = name;
);
getOrders()
return Promise.resolve((() =>
ORDERS.forEach((order: any) =>
const displayName = order;
console.log(order); // displayName exists in order
console.log(displayName); // Null
this.orderService.getOrdersByDisplayName(buildQuery(null, displayName, null, null))
.subscribe(
(data) =>
this.ordersWidgetInfo.push(
...order,
...data,
number: data.orders && data.orders.length,
);
,
error =>
console.log(`error for $orderType : $error`);
);
);
)());
angular typescript
add a comment |
I've a constant array of orders like this in constants.ts file
export const ORDERS = [
'id': 'PROCESSING',
'displayName': null
,
'id': 'SHIPPED',
'displayName': null
];
Here in my component I'm setting the value of 'displayName'
import ORDERS from './constants';
-----------------
--------------
setOrders()
ORDERS.forEach((order: any) =>
if(order.id === 'PROCESSING')
order.displayName = 'Processing';
if(order.id === 'SHIPPED')
order.displayName = 'Shipped';
);
But here when I try to get values of 'displayName' in another funtion it return null, but in the object order I can see the value of 'displayName'
getOrders()
ORDERS.forEach((order: any) =>
console.log(order); // displayName exists in the object
console.log(order.displayName); // But here null
);
Both functions are in a same component. Please help me how to resolve this thanks.
Update : Full implementation
getTimePeriod()
return Promise.resolve((() =>
this.orderService.getTimePeriod()
.subscribe(
(data) =>
this.orderStatusList = data.configParameters;
this.orderStatusList.forEach(
(s: any) =>
if (s.status === 'PROCESSING') this.setOrders(s.status, s.name);
if (s.status === 'SHIPPED') this.setOrders((s.status, s.name);
);
,
error =>
this.errorMessage = this.orderService.handleError(error);
);
)());
setOrders(status, name)
ORDERS.forEach((order: any) =>
if (order.id === status)
order.displayName = name;
);
getOrders()
return Promise.resolve((() =>
ORDERS.forEach((order: any) =>
const displayName = order;
console.log(order); // displayName exists in order
console.log(displayName); // Null
this.orderService.getOrdersByDisplayName(buildQuery(null, displayName, null, null))
.subscribe(
(data) =>
this.ordersWidgetInfo.push(
...order,
...data,
number: data.orders && data.orders.length,
);
,
error =>
console.log(`error for $orderType : $error`);
);
);
)());
angular typescript
I've a constant array of orders like this in constants.ts file
export const ORDERS = [
'id': 'PROCESSING',
'displayName': null
,
'id': 'SHIPPED',
'displayName': null
];
Here in my component I'm setting the value of 'displayName'
import ORDERS from './constants';
-----------------
--------------
setOrders()
ORDERS.forEach((order: any) =>
if(order.id === 'PROCESSING')
order.displayName = 'Processing';
if(order.id === 'SHIPPED')
order.displayName = 'Shipped';
);
But here when I try to get values of 'displayName' in another funtion it return null, but in the object order I can see the value of 'displayName'
getOrders()
ORDERS.forEach((order: any) =>
console.log(order); // displayName exists in the object
console.log(order.displayName); // But here null
);
Both functions are in a same component. Please help me how to resolve this thanks.
Update : Full implementation
getTimePeriod()
return Promise.resolve((() =>
this.orderService.getTimePeriod()
.subscribe(
(data) =>
this.orderStatusList = data.configParameters;
this.orderStatusList.forEach(
(s: any) =>
if (s.status === 'PROCESSING') this.setOrders(s.status, s.name);
if (s.status === 'SHIPPED') this.setOrders((s.status, s.name);
);
,
error =>
this.errorMessage = this.orderService.handleError(error);
);
)());
setOrders(status, name)
ORDERS.forEach((order: any) =>
if (order.id === status)
order.displayName = name;
);
getOrders()
return Promise.resolve((() =>
ORDERS.forEach((order: any) =>
const displayName = order;
console.log(order); // displayName exists in order
console.log(displayName); // Null
this.orderService.getOrdersByDisplayName(buildQuery(null, displayName, null, null))
.subscribe(
(data) =>
this.ordersWidgetInfo.push(
...order,
...data,
number: data.orders && data.orders.length,
);
,
error =>
console.log(`error for $orderType : $error`);
);
);
)());
angular typescript
angular typescript
edited Nov 12 at 14:18
asked Nov 12 at 13:26
Junaid
164211
164211
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The only way a bug like this would occur (other than the code being wrong) is with an invalid order of execution.
However, after trying your code, it seems to works just fine. See example on JSFiddle.
This leads me to conclude that your problem is most likely that getOrders
is being invoked before setOrders
, thus the null values. To fix this, make sure you are calling setOrders
before you call getOrders
.
This is bizzare. Although I'm returning promise and executing methods like thisthis.setOrders().then(() => this.getOrders());
– Junaid
Nov 12 at 13:48
Is there anywhere else in your code that these two methods are being called?
– Lansana
Nov 12 at 13:54
No just insidengOnInit()
– Junaid
Nov 12 at 13:59
Show your entire implementation ofsetOrders
. I assume the issue is inside there given all the information so far.
– Lansana
Nov 12 at 14:02
I've updated my question
– Junaid
Nov 12 at 14:19
|
show 8 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263184%2fangular-how-to-get-constant-values-in-the-component-after-setting-them%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The only way a bug like this would occur (other than the code being wrong) is with an invalid order of execution.
However, after trying your code, it seems to works just fine. See example on JSFiddle.
This leads me to conclude that your problem is most likely that getOrders
is being invoked before setOrders
, thus the null values. To fix this, make sure you are calling setOrders
before you call getOrders
.
This is bizzare. Although I'm returning promise and executing methods like thisthis.setOrders().then(() => this.getOrders());
– Junaid
Nov 12 at 13:48
Is there anywhere else in your code that these two methods are being called?
– Lansana
Nov 12 at 13:54
No just insidengOnInit()
– Junaid
Nov 12 at 13:59
Show your entire implementation ofsetOrders
. I assume the issue is inside there given all the information so far.
– Lansana
Nov 12 at 14:02
I've updated my question
– Junaid
Nov 12 at 14:19
|
show 8 more comments
The only way a bug like this would occur (other than the code being wrong) is with an invalid order of execution.
However, after trying your code, it seems to works just fine. See example on JSFiddle.
This leads me to conclude that your problem is most likely that getOrders
is being invoked before setOrders
, thus the null values. To fix this, make sure you are calling setOrders
before you call getOrders
.
This is bizzare. Although I'm returning promise and executing methods like thisthis.setOrders().then(() => this.getOrders());
– Junaid
Nov 12 at 13:48
Is there anywhere else in your code that these two methods are being called?
– Lansana
Nov 12 at 13:54
No just insidengOnInit()
– Junaid
Nov 12 at 13:59
Show your entire implementation ofsetOrders
. I assume the issue is inside there given all the information so far.
– Lansana
Nov 12 at 14:02
I've updated my question
– Junaid
Nov 12 at 14:19
|
show 8 more comments
The only way a bug like this would occur (other than the code being wrong) is with an invalid order of execution.
However, after trying your code, it seems to works just fine. See example on JSFiddle.
This leads me to conclude that your problem is most likely that getOrders
is being invoked before setOrders
, thus the null values. To fix this, make sure you are calling setOrders
before you call getOrders
.
The only way a bug like this would occur (other than the code being wrong) is with an invalid order of execution.
However, after trying your code, it seems to works just fine. See example on JSFiddle.
This leads me to conclude that your problem is most likely that getOrders
is being invoked before setOrders
, thus the null values. To fix this, make sure you are calling setOrders
before you call getOrders
.
answered Nov 12 at 13:34
Lansana
4,68022050
4,68022050
This is bizzare. Although I'm returning promise and executing methods like thisthis.setOrders().then(() => this.getOrders());
– Junaid
Nov 12 at 13:48
Is there anywhere else in your code that these two methods are being called?
– Lansana
Nov 12 at 13:54
No just insidengOnInit()
– Junaid
Nov 12 at 13:59
Show your entire implementation ofsetOrders
. I assume the issue is inside there given all the information so far.
– Lansana
Nov 12 at 14:02
I've updated my question
– Junaid
Nov 12 at 14:19
|
show 8 more comments
This is bizzare. Although I'm returning promise and executing methods like thisthis.setOrders().then(() => this.getOrders());
– Junaid
Nov 12 at 13:48
Is there anywhere else in your code that these two methods are being called?
– Lansana
Nov 12 at 13:54
No just insidengOnInit()
– Junaid
Nov 12 at 13:59
Show your entire implementation ofsetOrders
. I assume the issue is inside there given all the information so far.
– Lansana
Nov 12 at 14:02
I've updated my question
– Junaid
Nov 12 at 14:19
This is bizzare. Although I'm returning promise and executing methods like this
this.setOrders().then(() => this.getOrders());
– Junaid
Nov 12 at 13:48
This is bizzare. Although I'm returning promise and executing methods like this
this.setOrders().then(() => this.getOrders());
– Junaid
Nov 12 at 13:48
Is there anywhere else in your code that these two methods are being called?
– Lansana
Nov 12 at 13:54
Is there anywhere else in your code that these two methods are being called?
– Lansana
Nov 12 at 13:54
No just inside
ngOnInit()
– Junaid
Nov 12 at 13:59
No just inside
ngOnInit()
– Junaid
Nov 12 at 13:59
Show your entire implementation of
setOrders
. I assume the issue is inside there given all the information so far.– Lansana
Nov 12 at 14:02
Show your entire implementation of
setOrders
. I assume the issue is inside there given all the information so far.– Lansana
Nov 12 at 14:02
I've updated my question
– Junaid
Nov 12 at 14:19
I've updated my question
– Junaid
Nov 12 at 14:19
|
show 8 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263184%2fangular-how-to-get-constant-values-in-the-component-after-setting-them%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown