在 AngularJs 中使用 ngStyle 动态刷新背景图像 [英] Dynamic refresh background-image with ngStyle In AngularJs

查看:21
本文介绍了在 AngularJs 中使用 ngStyle 动态刷新背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 Angular 来动态改变 div 的 backgroundImage 时,我发现有两种方法可以设置背景图像:

When i use Angular to dynamic change div 's backgroundImage, i find there are two ways to set backgrond-image:

第一:

<div style="background:url({{example_expression}})"></div>

第二个:

<div ng-style="{backgroundImage: 'url({{example_expression}})'}"></div>

但是当我改变example_expression时,只有第一种方式可以动态改变backgroundImage.

But when i change example_expression, only the first way can dynamically change the backgroundImage.

Plunker

ngStyle 有什么问题?

推荐答案

ng-style 不应包含 {{}} 插值指令,可以直接访问作用域变量那里.另外 backgroundImage 应该是 'background-image'

ng-style should not contain {{}} interpolation directive, you could directly access scope variable there. Also backgroundImage should be 'background-image'

标记

<div ng-style="{'background-image': 'url('+ example_expression+')'}"></div>

演示 Plunkr

这篇关于在 AngularJs 中使用 ngStyle 动态刷新背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆