迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > Angular >

在 AngularJS 中显示图像

作者:迹忆客 最近更新:2023/05/09 浏览次数:

我们将通过示例介绍如何在 AngularJS 中添加图像。

在 AngularJS 中显示图像

图像是任何 Web 应用程序或网站中最重要的部分。我们将使用 ng-src 指令添加单个图像。

我们将讨论在 AngularJS 的 ng-repeat 指令中显示图像。ng-src 顺序与图像元素一起使用,以在 AngularJS 中显示来自模型的图像。

图像将保存在服务器上的文件夹中。它将通过将图像的路径设置为 AngularJS 中的 ng-src 指令来显示。

让我们通过一个示例并使用 ng-src 指令显示单个图像。

<html>
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('ngApp', [])
        app.controller('ngController', function ($scope) {
        });
    </script>
    <div ng-app="ngApp" ng-controller="ngController">
       <img alt="" ng-src="https://www.jiyik.com/ezoimgfmt/d33wubrfki0l68.cloudfront.net/7748c5aa61aa13fd4c346e3cbfebe49f2dd4d580/2738b/assets/img/logo.png?ezimgfmt=rs:187x36/rscb5/ng:webp/ngcb5"/>
    </div>
</body>
</html>

从上面的示例可以看出,在 AngularJS 应用程序中添加图像非常简单。

在 AngularJS 中显示多个图像

现在,让我们通过另一个示例并尝试从 JSON 添加多个图像。

下面的 HTML 标记包含一个 HTML div,已为其提供 ng-appng-controller 指令。HTML div 包含一个 HTML 按钮和由图像元素组成的表格,这些元素将使用 ng-repeat 指令从 JSON 数组中解决。

该按钮已被赋予 ng-click 指令。当按钮被点击时,控制器的通用表格函数被命名。

使用通用表函数创建 JSON 对象并将其分配给消费者 JSON 数组。

JSON 对象包含 consumerIdNamePhoto 字段。Photo 字段提供图像的 URL。

ng-repeat 指令名称暗示基于集合长度的元素。

在这个系统中,它将重复 tr 元素。HTML 的 tbody 元素已被指定为 ng-repeat 指令以重复消费者 JSON 数组的所有项目。

consumer JSON 数组中的每个 JSON 对象开发一个 tr 元素并将其附加到 HTML 表中。Photo 字段将分配给 ng-src 指令,该指令将从模型中读取图像 URL 并显示图像,如下所示。

<html>
<head>
    <title>Consumers</title>
</head>
<style>
    td{
        border: 1px solid black;
    }
</style>
<body>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('ngApp', [])
        app.controller('ngController', function ($scope) {
            $scope.IsVisible = false;
            $scope.GetData = function () {
                $scope.consumer= [
                { consumerId: 1, Name: "Google", Photo: "Images/1.jpg" },
                { consumerId: 2, Name: "迹忆客", Photo: "Images/2.jpg" },
                { consumerId: 3, Name: "Yahoo", Photo: "Images/3.jpg" },
               ];
                $scope.IsVisible = true;
            };
        });
    </script>
    <div ng-app="ngApp" ng-controller="ngController">
        <input type="button" value="Get Data" ng-click="GetData()" />
        <hr />
        <table cellpadding="0" cellspacing="0" ng-show="IsVisible">
            <tr>
                <th>Company Id</th>
                <th>Name</th>
                <th>Logo</th>
            </tr>
            <tbody ng-repeat="m in consumer">
                <tr>
                    <td>{{m.consumerId}}</td>
                    <td>{{m.Name}}</td>
                    <td><img alt="{{m.Name}}" ng-src="{{m.Photo}}"/></td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便