【问题标题】:Change Image Angular-UI Carousel更改图像 Angular-UI 轮播
【发布时间】:2017-07-18 12:50:35
【问题描述】:

我已经创建了 https://angular-ui.github.io/bootstrap/#/carousel 中给出的 Angular-UI Carousel

但问题是我想将图像更改为系统中本地可用的图像。

那么,我该如何实现呢?

这是我的轮播代码的 HTML 部分。

<div ng-controller="HomeController">
    <div style="height: 500px">
        <uib-carousel active="active" interval="myInterval" no-wrap="noWrapSlides">
            <uib-slide ng-repeat="slide in slides track by slide.id" index="slide.id">
                <img ng-src="{{slide.image}}" style="margin:auto;">
            </uib-slide>
        </uib-carousel>
    </div>
</div>

这是控制器代码:

angular.module('portfolioApp', ['ngAnimate','ngTouch','ui.bootstrap'])
 .controller('HomeController', ['$scope',function($scope){
    $scope.myInterval = 5000;
    $scope.noWrapSlides = false;
    $scope.active = 0;
    var slides = $scope.slides = [];
    var currIndex = 0;

    $scope.addSlide = function() {
        var newWidth = 600 + slides.length + 1;
        slides.push({
            image: 'http://lorempixel.com/' + newWidth + '/480',
            text: ['Nice image','Awesome photograph','That is so cool','I love that'][slides.length % 4],
            id: currIndex++
        });
    };

    $scope.randomize = function() {
        var indexes = generateIndexesArray();
        assignNewIndexesToSlides(indexes);
    };

    for (var i = 0; i < 4; i++) {
        $scope.addSlide();
    }

    // Randomize logic below

    function assignNewIndexesToSlides(indexes) {
        for (var i = 0, l = slides.length; i < l; i++) {
            slides[i].id = indexes.pop();
        }
    }

    function generateIndexesArray() {
        var indexes = [];
        for (var i = 0; i < currIndex; ++i) {
            indexes[i] = i;
        }
        return shuffle(indexes);
    }

    // http://stackoverflow.com/questions/962802#962890
    function shuffle(array) {
        var tmp, current, top = array.length;

        if (top) {
            while (--top) {
                current = Math.floor(Math.random() * (top + 1));
                tmp = array[current];
                array[current] = array[top];
                array[top] = tmp;
            }
        }

        return array;
    }
}]);

【问题讨论】:

  • 您要显示的图片具体在哪里?它们与运行 Web 应用的浏览器在同一台机器上吗?
  • 我想替换 url,并且我在我的 web 应用程序目录的同一目录中有自己的图像。我想在我的轮播中使用这些图片。

标签: angularjs html angular-ui-bootstrap angular-ui angular-carousel


【解决方案1】:

对于您要添加的每个图像,调用

slides.push({ image: image_filename, text: image_text, id: currIndex++ });

其中 image_filename 是图像的文件名,image_text 是要在图像上打印的文本

【讨论】:

  • 我想在幻灯片而不是图像中使用 ng-include 添加 html 页面。知道如何在幻灯片中推送 html 页面吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-05
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多