【问题标题】:How to change ng-src of an image on click如何在点击时更改图像的 ng-src
【发布时间】:2017-10-04 05:40:02
【问题描述】:

我正在尝试更改我在每个专辑中拥有的重复图像列表的 ng-src。我尝试通过增加缩略图 url 的大小来做一些笨拙的事情,但我想要一种更具体的方法将我的 imgs 的 ng-src 交换为图像的更大 url。这是我的代码:

<!DOCTYPE html>
<html>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <head>
        <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script type="text/javascript" src="album.js"></script>
        <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
        <link rel="stylesheet" href="album.css">
    </head>
    <body>

        <div ng-app="myApp" ng-controller="AlbumCtrl">
            <div class="bar">
                <h3>Search by ID or Title</h3><input type="text" class="search" ng-model="q" placeholder="Enter your search terms" />
            </div>
            <div ng-repeat="album in albumData|filter:q" id="thumbWrapper">
                <h1>{{album.id}}</h1> 
                <h4>Title: </h4><button class="show" ng-click="showme = !showme">{{album.title}}</button>
                <div id="thumbList"ng-show="showme"class="albumContent">
                    <ul ng-controller="PhotoCtrl" id="thumbList">
                        <li ng-repeat="photo in photoData" ng-if="album.userId == photo.albumId">
                            <img id="view{{$index}}" ng-click="zoom($index)" ng-src={{photo.url}}>
                            <p ng-click="delete($index)">x</p>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

js:

var app = angular.module('myApp', []);
app.controller('AlbumCtrl', function ($scope, $http) {
    $http.get("https://jsonplaceholder.typicode.com/albums").then(function(response) {
        $scope.albumData = response.data;
        console.log($scope.albumData);
    });
});
app.controller('PhotoCtrl', function($scope, $http) {
    $http.get("https://jsonplaceholder.typicode.com/photos").then(function(response) {
        $scope.photoData = response.data;

    });
    $scope.delete = function(index) {

        $scope.photoData.splice(index, 1);

        var del = "/" + (index + 1);
        $http.delete("https://jsonplaceholder.typicode.com/photos" + del).then(function(response) {
            console.log($scope.albumData);
        });
    };

    $scope.zoom = function(index) {
        var elem = "view" + index;
        var imageId = document.getElementById(elem);
        if(imageId.style.width == "1000px"){
        imageId.style.width = "600px";
        imageId.style.height = "600px";
        }else{
        imageId.style.width = "1000px";
        imageId.style.height = "1000px";  
        }
    };

});

非常感谢任何帮助

【问题讨论】:

标签: javascript jquery html angularjs image


【解决方案1】:

如果您只想更改 URL,则可以将 photo 对象传递给您的函数,如下所示:

&lt;img id="view{{$index}}" ng-click="zoom(photo)" ng-src={{photo.url}}&gt;

然后在您的JS 代码中,您可以在一行中更改该对象的 URL,如下所示:

$scope.zoom = function(photo) {
    photo.url = "http://your-new-url"  ;
};

更新

如果你只是想在ng-src属性中切换photo.urlphoto.thumbnailUrl,那么你可以使用这个方法。

&lt;img id="view{{$index}}" ng-click="photo.zoomed= !photo.zoomed" ng-src="{{photo.zoomed ? photo.thumbnailUrl : photo.url}}"&gt;

在这种情况下您不需要执行任何功能:)

DEMO (jsfiddle.net)

【讨论】:

  • 嗨,我尝试过类似的方法,但我有一个对象数组,我需要为其切换缩略图,所以我像这样使用它:$scope.zoom = function(index , obj) { var i = 索引; obj.url = $scope.photoData[0].url; };但它似乎没有工作。非常感谢任何帮助:)
  • 确保等到http 请求完成。请发布您的HTML 代码,因为您使用了$index,而我不在zoom 函数中使用。还要确保在浏览器中打开console 以检查是否有任何错误。
  • 浏览器代码 atm 中没有任何错误,但这是我的部分 html:
    • x

  • 当您在HTML 中使用ng-src="photo.thumbnailUrl" 时,您应该在zoom 函数中使用obj.thumbnailUrl 而不是obj.url。因为当按下按钮时photo.url 更新了,但你实际上并没有使用它。
  • 这是我如何处理问题的重写。我希望它在单击时在缩略图之间切换,所以我写了这个 if else 表达式:var current = false; if (current == false) { obj.thumbnailUrl = $scope.photoData[index].url; console.log(obj.thumbnailUrl) 当前=真; } else if (current == true) { obj.thumbnailUrl = $scope.photoData[index].url;控制台.log(obj.thumbnailUrl); current = false;} 问题是 console.logs 是一样的,不知道如何解决这个问题
【解决方案2】:

在 html 中将对象作为参数传递给缩放功能

 <img id="view{{$index}}" ng-click="zoom($index,photo)" ng-src={{photo.url}}> 

然后在缩放功能中随意更改网址

 $scope.zoom = function(index,obj) {

        obj.url = "whatever your url"


 };

【讨论】:

  • 不鼓励在控制器中使用 DOM,这是反模式,如果出现这种情况.. 我很乐意建议 OP 使用ng-style/ng-class
  • @Pankaj Parkar 没有得到它。从控制器更改图像 src 不是反模式
  • 这本来可以通过更改 photo.url 来实现,我想你明白我的意思了......
  • 我强烈推荐你阅读这个角度controller documentation,请特别阅读不要使用控制器来:小节的第一点..
  • 嘿,对于 obj.url,我会做 obj.url = $scope.photoData.url 从我的 json 对象中获取正确的索引图像 url 吗?我试着照原样做,似乎没有任何改变。
猜你喜欢
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 2011-06-16
  • 1970-01-01
  • 2018-04-14
  • 1970-01-01
  • 2012-09-28
相关资源
最近更新 更多