如何以角度显示图像?

问题描述 投票:0回答:4

如何以角度显示图像?


<div><img  class="images" ng-src="{{./resources/img/new.png}}"/></div>

<div><img  class="images" src="./resources/img/new.png"/></div>```
angularjs
4个回答
6
投票

学习角度模板绑定(https://angular.io/guide/template-syntax)。 如果您在本地有图像(./resources/img/new.png),您可以使用普通的 html 源代码

<img  class="images" src="./resources/img/new.png"/>

或者你的图像来自服务器或动态的那个时候你必须绑定它

img = this.imageSource
<img  class="images" src={{img}}/> or
<img class="images" [src]="img"/>

4
投票

Angular CLI 如何处理图像

还记得我们使用

npm run build
ng build --prod
命令吗? Angular CLI 将我们所有的资产都移到了
dist
文件夹中。当它看到资产文件夹中有图像时,它会做同样的事情。

我们所要做的就是在我们的模板中引用这些图像,路径从 src 文件夹开始。

例如,如果我们在

src/assets/img/logo.png,
处有一张图片,我们会将其添加到我们的模板中:

模板:


  <img src="assets/img/logo.png">
将图像添加到资产文件夹 Angular CLI 为我们生成的资产文件夹是用于存储图像的完美位置。

让我们从 angular.io 获取 Angular 标志

https://angular.io/assets/images/logos/angular/[email protected]

1
投票

为您的问题考虑简单易行的解决方案。

更复杂的代码会更加混乱。

Angular js 文件:

$scope.imageAddress='resources/img/new.png';

// The Path has to be exact.
// Check your project folder hierarchy for that.

HTML 文件:

<body ng-app = "myApp">
    <div ng-controller="myController">
        <img alt="" ng-src="{{ imageAddress }}" />
    </div>
</body>

您的路径将根据您编写图像源代码的 HTML 文件。

例如:- 如果您的 ng-src 在 Index.html 路径中:resources/index.html

你的 new.png 在路径中:resources/views/images/new.png

然后$scope.imageAddress='views/images/new.png';

还有一件事: 比 src 更喜欢 ng-src


0
投票

安装serve-static

npm install --save @nestjs/serve-static

然后,在 app.module.ts (Nestjs) 中:

import { ServeStaticModule } from '@nestjs/serve-static';
import {  resolve } from 'path';  
.... 
@Module({
  imports: [....,
        ServeStaticModule.forRoot(
      (() => {
        const publicDir = resolve('uploads');
        return {
          rootPath: publicDir,
          serveRoot: '/public',
          exclude: ['/api*'],
        };
      })()
    ),]

使用 resolve('mydir') 访问存储文件的 url 并查看 ex。 http://localhost:3000/public/image.png

© www.soinside.com 2019 - 2024. All rights reserved.