gpt4 book ai didi

javascript - Angular js 中与 ng-src 的两种方式绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-04 16:10:54 25 4
gpt4 key购买 nike

调用 changeMainMedia 函数时,当前值应更改,然后 ng-src="{{array[current].src}}" 也应该改变,但它不起作用。任何帮助将不胜感激。

<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;
}
}]);

最佳答案

您应该更改当前范围变量

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

应该是

scope.changeMainMedia = function(index) {
scope.current = index;
}
<小时/>

相反,当用户单击像 ng-click="selectedImage = obj"这样的图像时,您可以在另一个作用域变量中选择图像

标记

<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>

关于javascript - Angular js 中与 ng-src 的两种方式绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41509842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com