css - 无法让背景图片在 stacklayout 上工作

标签 css angular image typescript nativescript

我有一个 JSON 文件,它以 base64 格式返回任意数量的图像。在我的 component.html 中,我尝试将图像设置为 StackLayout 的背景,如下所示:

<StackLayout height="200" *ngFor="let image of selectedProperty.images">
   <StackLayout height="100%" ng-style="{'background-image':'url('+image.base64+')'}"></StackLayout>
   <!--<Image src="{{ image.base64 }}"></Image>-->
</StackLayout>

我知道image.base64保存正确的 base64 字符串,因为如果我取消注释 <Image>元素,它可以很好地显示图像。问题似乎是 background-image css 属性本身,因为即使我尝试使用来自谷歌的普通图像(例如 style="background-image:url('https://pbs.twimg.com/profile_images/839721704163155970/LI_TRk1z_400x400.jpg')" ), 它也不起作用。我感觉非常愚蠢,因为这必须是非常简单的事情,但我就是想不通。

非常感谢任何帮助。

最佳答案

我在我的一个元素中做过类似的事情。我将一个 div 设置为隐藏,直到图像加载完毕。为了跟踪此事件,我正在收听 load img 上的事件标签。

<!-- the div is hidden until the image is loaded -->
<div *ngIf="photo.url" class="image" [hidden]="loading"
     [ngStyle]="{'background-image': 'url(' + photo.url + ')'}" >
</div>
<!-- hidden img tag to load the image and toggle loading status -->
<img src="{{photo.url}}" alt="{{photo.description}} photo" hidden (load)="imageLoaded()">

在 Controller 中:

imageLoaded() {
  this.loading = false;
}

因此,当浏览器提取准备好呈现图像时,事件处理程序设置 loading成为false ,更新 View ,以及 <div>不再隐藏,这导致它呈现。

我假设您可以调整代码以使用 NativeScript,但如果不行请告诉我。

关于css - 无法让背景图片在 stacklayout 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48695854/

相关文章:

css - div中的图像位置

ios - 在 React Native 中动态需要图像

javascript - 悬停时显示选项卡式内容

Angular cli - 如何在 ng 服务时禁用自动重新加载

angular - 如何让 CORS 与 ASP.Net Core Web API 服务器 Angular 2 客户端一起工作

angular - 增强第三方库 Typescript

html - 使用 Angular 4 渲染 blob 图像

css - 如何正确设置 transform-origin 以创建完美的 X 符号?

css - 绝对定位的元素即使在相对容器中也移动

html - 选择元素上的省略号在 IE9 和 Chrome 中不起作用