As a beginner in Javascript, I am working with two models known as Unit
and Site
.
Each Unit
can have multiple Sites
, and this is how I pass them from the view:
<a href="#" ng-click="destroyUnit(unit.id, unit.sites)" ></a>
Below is the function defined in my AngularJS controller:
$scope.destroyUnit = function(unit_id, sites){
console.log(unit_id)
$scope.$emit("units.delete", unit_id, sites);
}
$scope.$on("units.delete", function(e, unit_id, sites){
console.log("unit_id: "+unit_id+", site_length: "+sites.length+", site_id "+sites[0].id);
});
However, the output I receive shows:
unit_id: 439, site_length: 2, site_id undefined
I'm curious about why the site_id
turns out to be undefined
, and how can I retrieve its value. Thank you.
Update
To clarify further; I simply want to eliminate the sites
from the unit
; specifically the site.id=443
update
I attempted using sites[0][0].id
but it resulted in an error:
TypeError: Cannot read property '0' of undefined