1

Upon calling changeMainMedia function, current value should change and then ng-src="{{array[current].src}}" should also change but it is not working. Any help would be appreciated.

<div id="media-gallery" class="small reveal text-center media-gallery" data-reveal>
    <div class="modal-body">
        <div class="main-media">
            <img ng-bind="current" class="main-gallery media-gallery-main" ng-src="{{array[current].src}}" / </div>

            <hr>

            <div class="nested-media">
                <img ng-click="changeMainMedia($index)" ng-repeat="obj in array" class="thumbnail media-gallery-thumbnail" ng-src="{{obj.src}}" />
            </div>
        </div>

        <button class="close-button" data-close aria-label="Close reveal" type="button">
       <span aria-hidden="true">x</span>
    </button>
    </div>
</div>

<script>
    var app = angular.module("mediaGallery", []);
    app.controller("mediaGalleryCtrl", ['$scope', function(scope) {
        var array = [];
        for (var i = 1; i < 10; i++) {
            array.push({
                src: "gallery/image (" + i + ").jpg"
            });
        }

        scope.array = array;
        scope.current = 0;

        scope.changeMainMedia = function(index) {
            this.current = index;
        }
    }]);

1 Answer 1

1

You should be change current scope variable

scope.changeMainMedia = function(index) {
    this.current = index
}

should be

scope.changeMainMedia = function(index) {
    scope.current = index;
}

Rather you could have selected image inside another scope variable, when user clicks on image like ng-click="selectedImage = obj"

Markup

<div class="main-media">
    <img ng-bind="current" class="main-gallery media-gallery-main" 
      ng-src="{{selectedImage.src}}"/> 
    <hr>
    <div class="nested-media">
        <img ng-click="selectedImage = obj" ng-repeat="obj in array" class="thumbnail media-gallery-thumbnail" ng-src="{{obj.src}}" />
    </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.