无法在我的角度模板中获取对象内部的值

时间:2016-12-11 02:38:32

标签: javascript angularjs arrays json ionic-framework

所以我在我的控制器中有这个代码:

$scope.loadContacts = function () {
  $scope.contacts = [];
  document.addEventListener("deviceready", function () {
    var options = {};
    options.multiple = true;
    $cordovaContacts.find(options).then(function (allContacts) {
      for (var i = 0; i < allContacts.length; i++) {
        var contact = allContacts[i];
        if (contact.phoneNumbers != null)
          $scope.contacts.push(contact);
      }
    }, function (error) {
      console.log("ERROR: " + error);
    })
  });
}

所以我对该代码没有错误。在我的模板中:

<ion-item class="item-text-wrap" ng-repeat="c in contacts">{{c.displayName}} - {{c.phoneNumbers}}
</ion-item>

输出:

enter image description here

正如你所看到的,虽然我得到了物品,但它仍然有效。所以我试过

<ion-item class="item-text-wrap" ng-repeat="c in contacts">{{c.displayName}} - {{c.phoneNumbers.value}} // .value since I want to get the cellphone numbers
</ion-item>

但没有运气。我得到一个空列表意味着它有错误。如何正确访问模板中的手机号码?

1 个答案:

答案 0 :(得分:1)

你的phoneNumbers是一个数组,如果你想获得第一个对象的手机号码,这应该可行

<ion-item class="item-text-wrap" ng-repeat="c in contacts">{{c.displayName}} - {{c.phoneNumbers[0].value}} </ion-item>