javascript - Pass var to angular $resource success function -
i've been going crazy on , think answer out there don't know right way ask google question.
essentially need way make $resource call, , pass in data want use in success function.
app.controller('variantlistcontroller', ['djresource', function(djresource){ var variants = djresource('/ship-builder/variants/?format=json'); var vehicle = djresource('/ship-builder/vehicles/:id', {id: '@id'}); this.variants = variants.query(function(variants){ $(variants).each(function(){ console.log(this); variantdata = this; var vehicledata = vehicle.get({id:this.basevehicle}, function(){ console.log(variantdata); }) }) }); }]);
in above example, in innermost success function, 'variantdata' value of last entry previous level. makes sense because value set last item in array long before success happens. need way though have value of 'variantdata' inexistince when vehicle.get() called.
does make sense? find hard explain issue.
you need create closure make work. like
this.variants = variants.query(function(variants){ $(variants).each(function(){ getvehicledata(this); }) }); function getvehicaldata(variantdata) { var vehicledata = vehicle.get({id:variantdata.vehicleid}, function(){ console.log(variantdata); }) }
Comments
Post a Comment