Promise返回undefined

时间:2017-10-20 17:52:47

标签: javascript angularjs angular-promise

我正试图从这个承诺中获取一个值,但它在我的模板上继续返回undefined。如果你看看我的getLabel函数,我在参数中获得了一个承诺的标签。我正在解析它并将其返回到我最初调用它的getMenuItems函数中的标题。当我在页面上看到它时,它会以未定义的形式返回。我需要一些帮助来理解原因。

angular.module("peanutModule").service("navigationService", ["registrationLink", "auctionLink", "webLabel", function(registrationLink, auctionLink, webLabel) {
  this.getMenuItems = function(menu) {
    return [{
      "title": "VIEW EVENT ITEMS",
      "href": auctionLink
    }, {
      "title": "REGISTER FOR " + getLabel(webLabel),
      "href": registrationLink
    }];
  };

  function getLabel(label) {
    var original = Promise.resolve(label);
    var cast = Promise.resolve(original);
    cast.then(function(value) {
      console.log('value: ' + value);
      return value;
    });
    console.log('original === cast ? ' + (original === cast));
  }

}]).service("ticketItemsService", ["APIDataProvider", "sharedOrganization", "ItemsCountFactory", function(APIDataProvider, sharedOrganization, ItemsCountFactory) {
  var dataProvider = new APIDataProvider("ItemsCount");
  dataProvider.setModelFactory(ItemsCountFactory);

  this.get = function() {
    return sharedOrganization.get().then(function(organization) {
      return dataProvider.get("organizations/" + organization.org_id + "/items");
    });
  };
}]).factory("registrationLink", ["slug", function(slug) {
  // return "https://test.net/register.php/?id=" + slug;
  return "https://test.net/thezone/" + slug + "/register";
}]).factory("auctionLink", ["slug", function(slug) {
  return "https://test.net/thezone/" + slug + "/items";
}]).factory("webLabel", ["sharedOrganization", function(sharedOrganization) {

  this.get = function() {
    return (isValidOrganization() ? $q.resolve : stackPromise)(org_data);
  };

  return sharedOrganization.get().then(function(organization) {
    return organization.web_label;
  });
}]);

1 个答案:

答案 0 :(得分:-1)

我设法弄明白了。我设置了api以便我可以调用api从db获取实际值,然后通过XMLHTTPRequest对我的db列进行同步调用。

.factory("webLabel",["slug", function (slug) {
var request = new XMLHttpRequest();
request.open('GET', '/api/organizations/' + slug + '/label', false); 
request.send(null);

var label = request.response;

return label;