Angular 2 how to update Observables arrays?Dynamically add event listenerHow do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I redirect to another webpage?How to check whether a string contains a substring in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?

Implement Homestuck's Catenative Doomsday Dice Cascader

What happens to foam insulation board after you pour concrete slab?

Working in the USA for living expenses only; allowed on VWP?

Is it legal in the UK for politicians to lie to the public for political gain?

Do adult Russians normally hand-write Cyrillic as cursive or as block letters?

What happened to all the nuclear material being smuggled after the fall of the USSR?

Why did a party with more votes get fewer seats in the 2019 European Parliament election in Denmark?

Calling GPL'ed socket server inside Docker?

Will TSA allow me to carry a Continuous Positive Airway Pressure (CPAP)/sleep apnea device?

Why is c4 bad when playing the London against a King's Indian?

My coworkers think I had a long honeymoon. Actually I was diagnosed with cancer. How do I talk about it?

X-shaped crossword

What's the correct term for a waitress in the Middle Ages?

Opposite of "Squeaky wheel gets the grease"

What are they doing to this rocket following its test fire?

Why don't B747s start takeoffs with full throttle?

What happens if you do emergency landing on a US base in middle of the ocean?

Word for a small burst of laughter that can't be held back

Dynamically loading CSS files based on URL or URI in PHP

Do manufacturers try make their components as close to ideal ones as possible?

How to supress loops in a digraph?

Identification quotas - TIKZ LaTeX

Accidentally renamed tar.gz file to a non tar.gz file, will my file be messed up

What risks are there when you clear your cookies instead of logging off?



Angular 2 how to update Observables arrays?


Dynamically add event listenerHow do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I redirect to another webpage?How to check whether a string contains a substring in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Stack: Angular,Nativescript, Typescript.



I am getting id's from localStorage and then for each id I am calling HTTP request to get that item from API. Everything works fine, but I want to make that both (LocalStorageArray of id's and Items from Http request) would be subscriptions and they would watch for changes. For example: if there are new localStorage id, it would call HTTP from that id and then add to my observable array.



My example works but not as I want, because it isn't updating my ObservableArray after I add an item to my localStorage. I searched for answers on StackOverflow how to subscribe to the array and call a function every time it changes, but neither helped. Links I viewed :



https://docs.nativescript.org/angular/ui/professional-ui-components/ng-ListView/getting-started



https://appdividend.com/2018/12/08/angular-7-observables-tutorial-with-example/



https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Responding_to_storage_changes_with_the_StorageEvent



My example :



Home.component.ts :



data;
public filteredItems : ObservableArray<Observable> = new ObservableArray([]);
localStorageArray = [];


 public getStorageArray()
this.localStorageArray = new Array;
console.log(localStorage.length)
for (let i = 0; i < localStorage.length; i++)
let key = localStorage.key(i);
let val = localStorage.getItem(key);
this.localStorageArray.push(val);

console.log('Local storage array', this.localStorageArray);
if(this.localStorageArray.length != 0)
this.getFavorites(this.localStorageArray);



getFavorites(eanArray)
if(this.localStorageArray.length > 1)
this.data = this.dataService.getProductsByEan(eanArray).subscribe
((data: any) =>
this.filteredItems = data['hydra:member'];
console.log(this.filteredItems.length);
, (error) =>
console.log(error)
, () =>
console.log('favorites succes')
);




And then I am trying to update this.filteredItems from another component :



List.component.ts :



providers: [HomeComponent],
constructor(private home: HomeComponent)


1.nothing happens, it isnt updating :



updatefilteredItems(item)
console.log(this.home.filteredItems.length) --> 0 , but it has items.
this.home.filteredItems.push(item)
console.log(this.home.filteredItems.length) --> 1 , but it has more items.



  1. nothing happens, it isnt updating :

updatefilteredItems()
this.home.getStorageArray();




I expected that when I update this.filteredItems it would update it, but it didn't.










share|improve this question
























  • So why not just subscribing to every id?

    – Hagai Wild
    Mar 24 at 14:11











  • I was trying , but I only know how to subscribe to one value at the time, not the whole localStorage , would you help me to understand how to subscribe to every id ?

    – Andrėjus Lazauskas
    Mar 24 at 14:12












  • please look here for the 'storage' event: developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/…

    – Hagai Wild
    Mar 24 at 14:16











  • I found "Responding to storage changes with the StorageEvent" this topic in your link, but it isn't really explains how to make it with typescript

    – Andrėjus Lazauskas
    Mar 24 at 14:20











  • stackoverflow.com/questions/35080387/…

    – Hagai Wild
    Mar 24 at 14:28

















0















Stack: Angular,Nativescript, Typescript.



I am getting id's from localStorage and then for each id I am calling HTTP request to get that item from API. Everything works fine, but I want to make that both (LocalStorageArray of id's and Items from Http request) would be subscriptions and they would watch for changes. For example: if there are new localStorage id, it would call HTTP from that id and then add to my observable array.



My example works but not as I want, because it isn't updating my ObservableArray after I add an item to my localStorage. I searched for answers on StackOverflow how to subscribe to the array and call a function every time it changes, but neither helped. Links I viewed :



https://docs.nativescript.org/angular/ui/professional-ui-components/ng-ListView/getting-started



https://appdividend.com/2018/12/08/angular-7-observables-tutorial-with-example/



https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Responding_to_storage_changes_with_the_StorageEvent



My example :



Home.component.ts :



data;
public filteredItems : ObservableArray<Observable> = new ObservableArray([]);
localStorageArray = [];


 public getStorageArray()
this.localStorageArray = new Array;
console.log(localStorage.length)
for (let i = 0; i < localStorage.length; i++)
let key = localStorage.key(i);
let val = localStorage.getItem(key);
this.localStorageArray.push(val);

console.log('Local storage array', this.localStorageArray);
if(this.localStorageArray.length != 0)
this.getFavorites(this.localStorageArray);



getFavorites(eanArray)
if(this.localStorageArray.length > 1)
this.data = this.dataService.getProductsByEan(eanArray).subscribe
((data: any) =>
this.filteredItems = data['hydra:member'];
console.log(this.filteredItems.length);
, (error) =>
console.log(error)
, () =>
console.log('favorites succes')
);




And then I am trying to update this.filteredItems from another component :



List.component.ts :



providers: [HomeComponent],
constructor(private home: HomeComponent)


1.nothing happens, it isnt updating :



updatefilteredItems(item)
console.log(this.home.filteredItems.length) --> 0 , but it has items.
this.home.filteredItems.push(item)
console.log(this.home.filteredItems.length) --> 1 , but it has more items.



  1. nothing happens, it isnt updating :

updatefilteredItems()
this.home.getStorageArray();




I expected that when I update this.filteredItems it would update it, but it didn't.










share|improve this question
























  • So why not just subscribing to every id?

    – Hagai Wild
    Mar 24 at 14:11











  • I was trying , but I only know how to subscribe to one value at the time, not the whole localStorage , would you help me to understand how to subscribe to every id ?

    – Andrėjus Lazauskas
    Mar 24 at 14:12












  • please look here for the 'storage' event: developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/…

    – Hagai Wild
    Mar 24 at 14:16











  • I found "Responding to storage changes with the StorageEvent" this topic in your link, but it isn't really explains how to make it with typescript

    – Andrėjus Lazauskas
    Mar 24 at 14:20











  • stackoverflow.com/questions/35080387/…

    – Hagai Wild
    Mar 24 at 14:28













0












0








0








Stack: Angular,Nativescript, Typescript.



I am getting id's from localStorage and then for each id I am calling HTTP request to get that item from API. Everything works fine, but I want to make that both (LocalStorageArray of id's and Items from Http request) would be subscriptions and they would watch for changes. For example: if there are new localStorage id, it would call HTTP from that id and then add to my observable array.



My example works but not as I want, because it isn't updating my ObservableArray after I add an item to my localStorage. I searched for answers on StackOverflow how to subscribe to the array and call a function every time it changes, but neither helped. Links I viewed :



https://docs.nativescript.org/angular/ui/professional-ui-components/ng-ListView/getting-started



https://appdividend.com/2018/12/08/angular-7-observables-tutorial-with-example/



https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Responding_to_storage_changes_with_the_StorageEvent



My example :



Home.component.ts :



data;
public filteredItems : ObservableArray<Observable> = new ObservableArray([]);
localStorageArray = [];


 public getStorageArray()
this.localStorageArray = new Array;
console.log(localStorage.length)
for (let i = 0; i < localStorage.length; i++)
let key = localStorage.key(i);
let val = localStorage.getItem(key);
this.localStorageArray.push(val);

console.log('Local storage array', this.localStorageArray);
if(this.localStorageArray.length != 0)
this.getFavorites(this.localStorageArray);



getFavorites(eanArray)
if(this.localStorageArray.length > 1)
this.data = this.dataService.getProductsByEan(eanArray).subscribe
((data: any) =>
this.filteredItems = data['hydra:member'];
console.log(this.filteredItems.length);
, (error) =>
console.log(error)
, () =>
console.log('favorites succes')
);




And then I am trying to update this.filteredItems from another component :



List.component.ts :



providers: [HomeComponent],
constructor(private home: HomeComponent)


1.nothing happens, it isnt updating :



updatefilteredItems(item)
console.log(this.home.filteredItems.length) --> 0 , but it has items.
this.home.filteredItems.push(item)
console.log(this.home.filteredItems.length) --> 1 , but it has more items.



  1. nothing happens, it isnt updating :

updatefilteredItems()
this.home.getStorageArray();




I expected that when I update this.filteredItems it would update it, but it didn't.










share|improve this question
















Stack: Angular,Nativescript, Typescript.



I am getting id's from localStorage and then for each id I am calling HTTP request to get that item from API. Everything works fine, but I want to make that both (LocalStorageArray of id's and Items from Http request) would be subscriptions and they would watch for changes. For example: if there are new localStorage id, it would call HTTP from that id and then add to my observable array.



My example works but not as I want, because it isn't updating my ObservableArray after I add an item to my localStorage. I searched for answers on StackOverflow how to subscribe to the array and call a function every time it changes, but neither helped. Links I viewed :



https://docs.nativescript.org/angular/ui/professional-ui-components/ng-ListView/getting-started



https://appdividend.com/2018/12/08/angular-7-observables-tutorial-with-example/



https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Responding_to_storage_changes_with_the_StorageEvent



My example :



Home.component.ts :



data;
public filteredItems : ObservableArray<Observable> = new ObservableArray([]);
localStorageArray = [];


 public getStorageArray()
this.localStorageArray = new Array;
console.log(localStorage.length)
for (let i = 0; i < localStorage.length; i++)
let key = localStorage.key(i);
let val = localStorage.getItem(key);
this.localStorageArray.push(val);

console.log('Local storage array', this.localStorageArray);
if(this.localStorageArray.length != 0)
this.getFavorites(this.localStorageArray);



getFavorites(eanArray)
if(this.localStorageArray.length > 1)
this.data = this.dataService.getProductsByEan(eanArray).subscribe
((data: any) =>
this.filteredItems = data['hydra:member'];
console.log(this.filteredItems.length);
, (error) =>
console.log(error)
, () =>
console.log('favorites succes')
);




And then I am trying to update this.filteredItems from another component :



List.component.ts :



providers: [HomeComponent],
constructor(private home: HomeComponent)


1.nothing happens, it isnt updating :



updatefilteredItems(item)
console.log(this.home.filteredItems.length) --> 0 , but it has items.
this.home.filteredItems.push(item)
console.log(this.home.filteredItems.length) --> 1 , but it has more items.



  1. nothing happens, it isnt updating :

updatefilteredItems()
this.home.getStorageArray();




I expected that when I update this.filteredItems it would update it, but it didn't.







javascript angular typescript nativescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 14:23







Andrėjus Lazauskas

















asked Mar 24 at 14:08









Andrėjus LazauskasAndrėjus Lazauskas

578




578












  • So why not just subscribing to every id?

    – Hagai Wild
    Mar 24 at 14:11











  • I was trying , but I only know how to subscribe to one value at the time, not the whole localStorage , would you help me to understand how to subscribe to every id ?

    – Andrėjus Lazauskas
    Mar 24 at 14:12












  • please look here for the 'storage' event: developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/…

    – Hagai Wild
    Mar 24 at 14:16











  • I found "Responding to storage changes with the StorageEvent" this topic in your link, but it isn't really explains how to make it with typescript

    – Andrėjus Lazauskas
    Mar 24 at 14:20











  • stackoverflow.com/questions/35080387/…

    – Hagai Wild
    Mar 24 at 14:28

















  • So why not just subscribing to every id?

    – Hagai Wild
    Mar 24 at 14:11











  • I was trying , but I only know how to subscribe to one value at the time, not the whole localStorage , would you help me to understand how to subscribe to every id ?

    – Andrėjus Lazauskas
    Mar 24 at 14:12












  • please look here for the 'storage' event: developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/…

    – Hagai Wild
    Mar 24 at 14:16











  • I found "Responding to storage changes with the StorageEvent" this topic in your link, but it isn't really explains how to make it with typescript

    – Andrėjus Lazauskas
    Mar 24 at 14:20











  • stackoverflow.com/questions/35080387/…

    – Hagai Wild
    Mar 24 at 14:28
















So why not just subscribing to every id?

– Hagai Wild
Mar 24 at 14:11





So why not just subscribing to every id?

– Hagai Wild
Mar 24 at 14:11













I was trying , but I only know how to subscribe to one value at the time, not the whole localStorage , would you help me to understand how to subscribe to every id ?

– Andrėjus Lazauskas
Mar 24 at 14:12






I was trying , but I only know how to subscribe to one value at the time, not the whole localStorage , would you help me to understand how to subscribe to every id ?

– Andrėjus Lazauskas
Mar 24 at 14:12














please look here for the 'storage' event: developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/…

– Hagai Wild
Mar 24 at 14:16





please look here for the 'storage' event: developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/…

– Hagai Wild
Mar 24 at 14:16













I found "Responding to storage changes with the StorageEvent" this topic in your link, but it isn't really explains how to make it with typescript

– Andrėjus Lazauskas
Mar 24 at 14:20





I found "Responding to storage changes with the StorageEvent" this topic in your link, but it isn't really explains how to make it with typescript

– Andrėjus Lazauskas
Mar 24 at 14:20













stackoverflow.com/questions/35080387/…

– Hagai Wild
Mar 24 at 14:28





stackoverflow.com/questions/35080387/…

– Hagai Wild
Mar 24 at 14:28












0






active

oldest

votes












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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324639%2fangular-2-how-to-update-observables-arrays%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324639%2fangular-2-how-to-update-observables-arrays%23new-answer', 'question_page');

);

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







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해