使用 Angular (Ionic) 检索 Contentful 中的链接条目字段

Retrieving Linked Entry Fields in Contentful with Angular (Ionic)

我是 Contentful 和 Angular(在 Ionic 中工作)的新手,并且在整理如何从 Contentful 条目的相关内容(又名 "Link")中正确检索字段数据时遇到了一些困难。

就目前而言,我可以根据 'Terms' 工厂列出我的条目,并且可以移动到一个单独的条目状态,在该状态下我可以列出链接的内容——正如你在这里看到的——只是id。

我觉得这可以在 the/a 工厂解决,但不太确定如何进行。到目前为止,我尝试过的一切都没有奏效。而且,如果我的做法是错误的,我很高兴被告知。

非常感谢。

services.js

angular.module('app.services', ['ngResource'])

.factory('Terms', function ($resource, $log) {

  var apiGetEntries = 'https://cdn.contentful.com/spaces/SPACE_ID/entries?access_token=MY_ACCESS_TOKEN';

  var terms = $resource(apiGetEntries, null, {

    query: {
      method: 'GET',
      isArray: true,
      transformResponse: function(data, headers) {
        var entriesRaw = angular.fromJson(data);
        var entries = [];
        angular.forEach(entriesRaw.items, function(entry) {
          entries.push(entry);
        });
        console.log(entries);
        return entries;
      }
    }

  });

  return terms;

})

controllers.js

angular.module('app.controllers', [])

.controller('getTerms', function($scope, Terms) {

  $scope.terms = Terms.query();

})

.controller('getTermDetail', function($scope, $http, $stateParams) {

  var apiBasePath = 'https://cdn.contentful.com/spaces/SPACE_ID/entries/';
  var accessToken = 'MY_ACCESS_TOKEN';
  var termId      = $stateParams.termId;

  $http({
    url: apiBasePath + termId, 
    method: "GET",
    params: { access_token: accessToken }
  }).success(function(data, status, headers, config) {
      $scope.term = data;
      if(typeof $scope.term.fields.relatedTo != 'undefined') {
        $scope.relatedTerms = $scope.term.fields.relatedTo
      }
    }).error(function(data, status, headers, config) {
      console.log(data);
    })

});

templates/term-definition.html

<ion-view view-title="Definition">
  <ion-content class="padding">
    <h2>{{ term.fields.term }}</h2>
    <p>
      {{ term.fields.definition }}
    </p>
    <ion-list>
      <ion-item class="" ng-repeat="relatedTerm in relatedTerms" type="item-text-wrap" href="#/tab/terms/{{ relatedTerm.sys.id }}">
        {{ relatedTerm.sys.id }}
      </ion-item>
    </ion-list>
  </ion-content>
</ion-view>

除非您有非常具体的用例,否则您不需要手动完成所有这些操作。可以用官方的Contentful JS SDK or the third party Angular JS Contentful integration. Both of them deal with link resolution automatically. You can see more info about links here https://www.contentful.com/developers/docs/concepts/links/ and here https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/links

希望对您有所帮助:)