0

I have a piece of code inside an angular controller that requests three services for data. Once the last service returns, I'm taking the data from all and merge it into a dataset. Out of the nested loops, my "vm" instance of this have the values I want the way I want (using a $log.debug to the console) but when I try to access in the view it has the value of initialization. Here is my code:

function loadConfigs(userId) {
  // Load the apps the user have access to
  aimsApps.getAccessibleApps()
  .then((resApps) => {
    vm.apps = resApps.data;

    // Load notifications types
    aimsNotificationTypes.getNotificationTypes()
    .then((resTypes) => {
      vm.types = resTypes;

      // Load the configurations for a user
      aimsNotificationConfigs.getNotificationConfigs(userId)
      .then((resConfigs) => {
        vm.configs = resConfigs;

        // Create a store like notification[app][type] and load the configs
        $log.debug('ALL RESOLVED', vm.apps, vm.types, vm.configs);
        vm.apps.forEach((eApp) => {
          vm.notifSettings[eApp.name] = [];
          vm.types.forEach((eType) => {
            vm.configs.forEach((eConfig) => {
              if ((eConfig.app === eApp.name) && (eConfig.notificationType === eType.name)) {
                vm.notifSettings[eApp.name][eType.name] = eConfig;
              } else {
                vm.notifSettings[eApp.name][eType.name] = {
                  app: eApp.name,
                  user: userId,
                  notificationType: eType.name,
                  sendToWeb: true,
                  sendToEmail: true,
                  sendToSMS: true,
                };
              }
            });
          });
        });

        $log.debug('NOTIF CONFIG', vm.notifSettings);
      });
    });
  });
}

When this line $log.debug('NOTIF CONFIG', vm.notifSettings); is reached, I can see in the console the values for vm.notifSettings but my view doesn't reflect those changes. Any suggestions?

2
  • Could you create a plunker to demonstrate the problem. I think the issue must be linked on how you assign the variable vm.settings to your template. It is possible that you loose the reference to vm.settings Commented Mar 3, 2017 at 14:36
  • I assume you are creating vm.notifSettings[eApp.name] = []; array in the for each loop ? If so then it is not the way how you do it. As you are creating a scope object outside angular context and that too in http call.I would suggest to create your scope object outside service call and reset at the start of the loop Commented Mar 3, 2017 at 14:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.