再次:ng-click不在ng-if内部的ng-repeat内工作

时间:2017-05-03 14:02:29

标签: angularjs angularjs-scope angularjs-ng-repeat angularjs-ng-click angularjs-ng-if

这很可能是另一个Angular范围问题。我很努力地找到SO的解决方案,然而,我找到的任何东西都无法解决我的问题。

我将Angular.js与Swiper.js一起使用。在外部垂直滑块的一个滑块内,我有一个内部水平滑块,仅显示(ng-if)用户选择音频(不是视频);然后,用户可以在多个图像(ng-repeat)之间滑动。这些图片有我想点击的标题(ng-click),因此可以隐藏/显示(ng-show)。

除了点击后隐藏和显示外,一切正常。我尝试在几个代码级别(div)之间分发ng-directives,并尝试使用或不调用我的控制器函数。

这是我的代码:

<div ng-if="selectedMedium().class === 'Audio'" class="swiper-container swiper-container-images">
    <div class="swiper-wrapper">
        <div ng-repeat="image in selectedImages track by $index" end-repeat class="swiper-slide images">
            <img class="image" ng-src="{{ image }}">
            <a class="caption" href="#" ng-init="show = true" ng-show="show" ng-click="show = !show">{{ selectedCaptions[$index] }}</a>
        </div>
    </div>
    <div class="swiper-pagination"></div>
</div>

为了完整起见,我包括$ scope.selectedMedium函数(在控制器中)和end-repeat指令(在控制器外部与Angular和Swiper结合):

$scope.selectedMedium = function() {
    for (var i = 0; i < $scope.media.length; i++) {
        if ($scope.media[i].person === $scope.selectedPerson.person && $scope.media[i].topic === $scope.selectedTopic.topic) {
            $scope.selectedImages = ("imageFiles" in $scope.media[i]) ? $scope.media[i].imageFiles : null;
            $scope.selectedCaptions = ("imageSubtitles" in $scope.media[i]) ? $scope.media[i].imageSubtitles : null;
            return $scope.media[i];
        }
    }
    return "Error";
};

myApp.directive('endRepeat', ['$timeout', function($timeout) {
    return {
        restrict: 'A',
        link: function(scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function() {
                    scope.$emit('ngRepeatFinished');
                });
            }
        }
    }
}]);

在上面的代码中,我试图在没有任何引用控制器中的函数的情况下进行操作,但在其他版本中,我尝试通过放置类似的东西从(多个,我猜)childscope(s)重新连接到$ scope: / p>

<a class="caption" href="#" ng-show="$parent.isCaptionShowing[$index]" ng-click="$parent.isCaptionShowing[$index] = !$parent.isCaptionShowing[$index]">{{ selectedCaptions[$index] }}</a>

我甚至试过$parent.$parent.isCaptionShowing[$index]。对于这种方法,我在$ scope.selectedMedium函数中添加了以下行:

$scope.isCaptionShowing = ($scope.selectedCaptions !== null) ? new Array($scope.selectedCaptions.length).fill(true) : null;

这两种方法(所有HTML代码内部与对控制器功能的调用都没有)起作用。我似乎ng-click功能没有按预期工作。任何想法可能是什么问题?

0 个答案:

没有答案
相关问题