angularjs - angular-translate inside loop -
i'm trying construct translated message looping on array of objects , adding new "message" property object containing translated string. see correct message output while inside $translate.then(); when assign message object undefined. correct way resolve promise returned $translate.then() , assign "message" property?
//items.controller.js
function getitems() { return itemsfactory.getitems() .then(function (response) { vm.items = inititemslist(response.activities); }); } function inititemslist(itemslist) { (var = 0; < itemslist.length; i++){ var activitytype = itemslist[i].activitytype; switch (activitytype){ case "history": { var itemname = itemslist[i].item.itemname; var itemversion = itemslist[i].item.itemversion; $translate('activity.'+activitytype, { itemname: itemname, itemversion: itemversion }).then(function(content){ vm.itemcontent = content; console.log(vm.itemcontent); // correct message displayed. }); break; } default: { break; } } itemslist[i].message = vm.itemcontent; // undefined } return itemslist; }
// translation.json
"activity : { "history" : "history {{ itemname }} {{ itemversion }}." }
promises resolved asynchronously. statement
itemslist[i].message = vm.itemcontent;
, executed right after switch, executed before callback passed $translate promise. move statement callback:
$translate('activity.'+activitytype, { itemname: itemname, itemversion: itemversion }).then(function(content){ vm.itemcontent = content; console.log(vm.itemcontent); itemslist[i].message = vm.itemcontent; });
Comments
Post a Comment