2

我正在尝试使用 metafizzy 的 flickity 框架来动态显示内容,使用 angulars ng-repeat。

但由于某种原因,这些项目在加载到 DOM 时似乎从 flickity-viewport 中推出。任何人都知道为什么会发生这种情况以及如何避免它?

当像这样在其中显示静态内容时,画廊工作正常;

HTML:静态标记示例

<div ng-controller="FlickityCtrl">
   <div id="main-content" class="gallery js-gallery">
     <div class="gallery-cell"> Static Title </div>
     <div class="gallery-cell"> Static Title </div>
     <div class="gallery-cell"> Static Title </div>
  </div>

..它在尝试借助 Angular 的 ng-repeat 指令填充画廊时,画廊会中断。

HTML : 使用 NG-REPEAT 标记

<div ng-controller="FlickityCtrl" >
  <div id="main-content" class="gallery js-gallery">
    <div ng-repeat="chapter in chapters" ng-click="loadSubchapters(chapter.title)">
    <h1  class="gallery-cell cell-card-bg"> 
        {{ chapter.title | strip_namespace }} 
   </h1>
</div>
  </div>
  <hr>
     <button ng-click="loadChapters()" >Load chapters</button>
  <hr>
  <ul>
    <li ng-repeat="chapter in subchapters">
      {{ chapter.title | strip_namespace }}
    </li>
  </ul><br />
   <hr >
 </div>

JAVASCRIPT

angular.module('FlickityApp', [])
   .controller('flickityCtrl', ['$scope', '$timeout', function ($scope, $timeout) {

var updateUI = function(data) {
    if (!data || !data.query) { return; }
    $timeout(function() {
        $scope.chapters = data.query.pages;
        console.log(data);
    });
};

$scope.loadChapters = function() {
    mw.loader.using('mediawiki.api', function() {
        (new mw.Api()).get({
            action: 'query',
            generator: 'categorymembers',
            gcmtitle: 'Category:examplepage'
        }).done(function(data) {
            $timeout(function() {
                $scope.chapters = data && data.query ? data.query.pages : {};

            });
        });
    });
};

$scope.loadSubchapters = function(chapterTitle) {
    mw.loader.using('mediawiki.api', function() {
        (new mw.Api()).get({
            action: 'query',
            generator: 'categorymembers',
            gcmtitle: chapterTitle
        }).done(function(data) {
            $timeout(function() {
                $scope.subchapters = data && data.query ? data.query.pages : {};
            });
        });
      });
   };
}])


.filter('strip_namespace', ['$sce', function($sce){
return function(text) {
    text = text.split(":");
    return text.length > 1 ? text[1] : text[0];
   };
 }]);
.directive('flickity', [function() {
   return {
      restrict: 'E',
      templateUrl: 'templates/view.html',
      replace: true,
      scope: { chapters: '=' },
      link: function(scope, elem, attrs, ctrl) {
         scope.$watch('chapters', function() {
        elem.flickity({
        // settings
           });
         });
      }
   };
}]);
angular.element(document).ready(function() {
    angular.bootstrap(document, ['FlickityApp']);
    var flkty = new Flickity('.gallery');
});

链接到 flickity apihttp ://flickity.metafizzy.co/api.htm

4

0 回答 0