Как вернуть ответ на вызывающую функцию в AngularJS? - PullRequest
0 голосов
/ 01 июня 2018

Я вызываю этот метод testDataService, который определен в том же файле JS.Этот метод возвращает список, который он получает из вызова API.Я хочу установить это значение для модели и вернуть объект модели.но моя функция возврата вызывается до того, как мой API вернет ответ.Как я могу убедиться, что функция возврата будет вызываться только после вышеперечисленного, если блок получит данные из API.

       getTestData(model) {

            model.name = "practice";
            model.value = 2;

    // this if loop is calling an API and setting up data into model.

           if(this.model.form.length>0 && this.model.form[0].entityName === 'test'){
           let responseList;
           this.testDataService(this.model).then(response => {
           responseList = response;
           model.class = responseList;

    });
    }

           return this.model;

       // this return function is getting called before the api returns its data. How can i make sure this return will get called only after the above if block get the data from API.

    }



    This is method definition

        testDataService(model) {
          let profileType = "test";
          let profileId = "test_profile";
          let configCategory = "test";
          return this.referenceDataService.getTestData(profileType, profileId, configCategory);
        }

1 Ответ

0 голосов
/ 01 июня 2018

Вам нужно передать оператор возврата из обратного вызова, а не размещать его снаружи.

getTestData(model) {
  model.name = "practice";
  model.value = 2;

 // this if loop is calling an API and setting up data into model.

 if (this.model.form.length > 0 && this.model.form[0].entityName === 'test') {
    this.testDataService(this.model).then(response => {
        let responseList = response;
        model.class = responseList;
        return this.model;
    }).then(response => {
        return null;
    });
  }
}

testDataService(model) {
 let profileType = "test";
 let profileId = "test_profile";
 let configCategory = "test";
 return this.referenceDataService.getTestData(profileType, profileId, configCategory);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...