角度幻灯片到动画在ng-repeat中不起作用?

时间:2016-11-21 12:25:16

标签: javascript css angularjs animation angularjs-directive

嗨,我是角度动画的新手,我需要像这样click Here

动画div

根据上面的链接,我尝试动画到ng-repeat内的div,但我这样做时出错Uncaught Type Error: Cannot read property 'query Selector' of null(…)。 这是我尝试FIDDLE HERE

的链接

HTML

<div ng-app="angularSlideables"  ng-controller="myctrl">
<div ng-repeat="menu in menuArray">
<div slide-toggle="#derp" ng-click="showItem(menu.displayName);">
        <h2><b>{{menu.displayName}}</b></h2>
      </div>

    <div id="derp" class="slideable" ng-repeat="item in menu.items" ng-show="state">
         <span class="md-subhead"><i>{{item.shortDesc}}</i></span>
    </div>
  </div> 
</div>

控制器

angular.module('angularSlideables', [])
.controller('myctrl', function($scope){
$scope.menuArray = [{displayName:'jhon',items:[{shortDesc:'NYC'}]},{displayName:'rock',items:[{shortDesc:'canada'}]},{displayName:'punk',items:[{shortDesc:'Aus'}]}]
})
.directive('slideable', function () {
    return {
        restrict:'C',
        compile: function (element, attr) {
            // wrap tag
            var contents = element.html();
            element.html('<div class="slideable_content" style="margin:0 !important; padding:0 !important" >' + contents + '</div>');

            return function postLink(scope, element, attrs) {
                // default properties
                attrs.duration = (!attrs.duration) ? '1s' : attrs.duration;
                attrs.easing = (!attrs.easing) ? 'ease-in-out' : attrs.easing;
                element.css({
                    'overflow': 'hidden',
                    'height': '0px',
                    'transitionProperty': 'height',
                    'transitionDuration': attrs.duration,
                    'transitionTimingFunction': attrs.easing
                });
            };
        }
    };
})
.directive('slideToggle', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var target = document.querySelector(attrs.slideToggle);
            attrs.expanded = false;
            element.bind('click', function() {
                var content = target.querySelector('.slideable_content');
                if(!attrs.expanded) {
                    content.style.border = '1px solid rgba(0,0,0,0)';
                    var y = content.clientHeight;
                    content.style.border = 0;
                    target.style.height = y + 'px';
                } else {
                    target.style.height = '0px';
                }
                attrs.expanded = !attrs.expanded;
            });
        }
    }
});

1 个答案:

答案 0 :(得分:0)

主要问题是你在ng-repeat上有一个id。 Id是一个singelton值,意味着您不能拥有两个具有相同ID的元素。

试试:

<div id="derp">
  <div class="slideable" ng-repeat="item in menu.items">
    <span class="md-subhead"><i>{{item.shortDesc}}</i></span>
  </div>
</div>

它应该可以解决您所遇到的错误,并帮助您继续推进此功能。

如果有帮助,请告诉我:)。