AngularJS如何将服务的承诺结果返回给控制器

时间:2019-09-23 14:01:41

标签: angularjs promise

void DA_RowUpdating(object sender, SqlRowUpdatingEventArgs e) { if (e.Status == UpdateStatus.ErrorsOccurred) { e.Row.RowError = e.Errors.Message; e.Status = UpdateStatus.SkipCurrentRow; throw new Exception(e.Errors.Message); } <snip> } void DA_RowUpdated(object sender, SqlRowUpdatedEventArgs e) { try { if (e.Status == UpdateStatus.ErrorsOccurred) { var ex = new Exception(e.Errors.Message); throw ex; } <snip> 包含itemsService函数,该函数检查答应成功是否将项目添加到items数组并将其传递给additem控制器 没关系 现在,我还想存储承诺结果并将其传递给shoppinglist中的控制器 不能在dom中与list.status一起使用此属性,以在承诺结果为拒绝时显示错误

ng-if

控制器

(function() {
  'use strict';

  angular
    .module('shopping', [])
    .controller('shoppinglist', shoppinglist)
    .service('itemsService', itemsService)
    .service('weightControllerService', weightControllerService);

服务

  shoppinglist.$inject = ['itemsService'];
  function shoppinglist(itemsService) {
    let list = this;
    list.Quantity = '';
    list.Name = '';
    list.additem = () => {
      itemsService.additem(list.Quantity, list.Name);
      // 
      list.status = itemsService.status;
    };
    list.removeitem = $index => {
      itemsService.removeitem($index);
    };
    list.items = itemsService.getitems();
  }

服务

  itemsService.$inject=['weightControllerService']
  function itemsService(weightControllerService) {
    let service = this;
    let items = [];
    service.additem = (N, Q) => {
      let namePromise = weightControllerService.nameCheck(N);
      namePromise
        .then(resolve => {
          let item = { Name: N, Quantity: Q };
          items.push(item);
          // how can i return the resolve to show or hide error at dom
          service.status = resolve;
        })
        .catch(reject => {
          // how can i return the resolve to show or hide error at dom
          service.status = reject;
        });
    };
    service.removeitem = $index => {
      items.splice($index, 1);
    };
    service.getitems = () => {
      return items;
    };
  }

1 个答案:

答案 0 :(得分:0)

对于有诺言的人,将return语句用于已解决的诺言,并将throw语句用于已拒绝的诺言:

itemsService.additem = (N, Q) => {
  let namePromise = weightControllerService.nameCheck(N);
  var newPromise = namePromise
    .then(resolve => {
      let item = { Name: N, Quantity: Q };
      items.push(item);
      // how can i return the resolve to show or hide error at dom
      service.status = resolve;
      //RETURN value
      return items;
  })
    .catch(reject => {
      // how can i return the resolve to show or hide error at dom
      service.status = reject;
      //THROW reason
      throw reject;
  });
  //RETURN new promise
  return newPromise;
};

然后在控制器中使用返回的promise:

list.additem = () => {
  let promise = itemsService.additem(list.Quantity, list.Name);
  promise.then(items => {
      console.log(items);
  })
   .catch(reject => {
      console.log("additem failed");
      console.log(reject);
  });

  // 
  //list.status = itemsService.status;
};

.then.catch方法返回一个新的诺言,该诺言通过successCallbackerrorCallback的返回值(或抛出)来解决或拒绝(除非该值是一个承诺,在这种情况下,它将使用promise chaining来解决该承诺中所解析的值。

有关更多信息,请参见