getting first array item in angular js service array

<p>i have 2 services. 1 uses the other to fetch data, this works... After that the service provides a getter and setter for a variable</p><p>` .service('ClickHandlers', ['$firebase','FirebaseData',function ($firebase,FirebaseData) { var lastClickedVisitedPlace;</p><pre><br /><code> return {<br /><br /> getLastClickedVisitedPlace: function () {<br /> console.log('getLastClickedVisitedPlace service function triggered');<br /> var destinations = FirebaseData.getDestinations();<br /> console.log('service destinations',destinations);<br /><br /> **var defaultVisitedplace = destinations[0];<br /> console.log('service defaultVisitedplace',defaultVisitedplace);<br /> lastClickedVisitedPlace = defaultVisitedplace.id;**<br /> console.log('service lastClickedVisitedPlace',lastClickedVisitedPlace);<br /> return lastClickedVisitedPlace;<br /> },<br /> setLastClickedVisitedPlace:function(id){<br /> console.log('service id',id);<br /> console.log('setLastClickedVisitedPlace service function triggered');<br /> lastClickedVisitedPlace = id;<br /> console.log('service lastClickedVisitedPlace',lastClickedVisitedPlace);<br /> }<br /><br /> };<br />}])`<br /></code><br /></pre><p>the following lines: <code>var defaultVisitedplace = destinations[0]; console.log('service defaultVisitedplace',defaultVisitedplace); lastClickedVisitedPlace = defaultVisitedplace.id;</code> don't seem to work, destinations[0] is undefined, but in the line above saying: <code>var destinations = FirebaseData.getDestinations(); console.log('service destinations',destinations);</code></p><p>i get this as return data in console: <code>destinations [] 0: Object 1: Object 2: Object</code></p><p>so how can i get the first array object .id value like you would normally do with destinations[0].id ??</p><br /><h2>Click here view more answers <a href="%20http://stackoverflow.com/questions/34526209/getting-first-array-item-in-angular-js-service-array">http://ift.tt/1VpgpcL</a></h2>

Nhận xét

Bài đăng phổ biến từ blog này

Align div to bottom of the page in bootstrap

Align elements inside an tag vertically

Javascript is unable to stop page loading