简体   繁体   中英

angularjs combine two Trello api and assign it to $rootScope variable

Hello there I have a function that will combine two API from Trello.

if I console.log, it will give the result correctly:

在此输入图像描述

but I Would like to assign it's value to a $rootScope so I can use it to the component.

my .run() code:

angular.module('workTrello', [
  'ngRoute'
])
.config()
.run(function($rootScope){

    async function trelloCards() {
        let response = await fetch(`https://api.trello.com//1/boards/5ba38efef50b8979566922d0/cards?key=${key}&token=${token}`);
        return await response.json();
      }

    async function trelloLists() {
        let response = await fetch(`https://api.trello.com/1/boards/5ba38efef50b8979566922d0/lists?key=${key}&token=${token}`)
        return await response.json();
      }

    async function bindWorkInfo() {
      const cards = await trelloCards();
      const lists = await trelloLists();

      let trelloWorkData = [];


      for (let i = 0; i < lists.length; i++) {
        const list = lists[i];
        list.name = list.name.substr(0,list.name.indexOf(' '))
        let listWithCard = [];
        for (let x = 0; x < cards.length; x++) {
          const card = cards[x];
          if (card.idList == list.id) {
            try { /** 8-12+14-16 = 6*/
              card.name = Math.abs(eval(card.name));
              listWithCard.push({
                  id:list.id, date:list.name, idCard:card.id,
                  time:card.name, task:card.badges.checkItemsChecked,
                  idMember:card.idMembers[0]
              });
            } catch (error) {}
          }
        }
        trelloWorkData.push(listWithCard);
      }
      console.log(trelloWorkData)
      return trelloWorkData;
   }

   bindWorkInfo().then((res) => $rootScope.workedInfo = res);
}

this my attempt :

    bindWorkInfo().then((res) => $rootScope.workedInfo = res);

but when I access $rootScope.workedInfo from the component it will return as undefined.

anyone know the correct way of assigning it to $rootScope ?

you have to return a promise to make the function thennable. change the following return code

return trelloWorkData;

to

return Promise.resolve(trelloWorkData); 

and try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM