0

ng-style で画像を条件付きで表示しようとしています。ここ(およびドキュメント) にリストされているすべての提案を試しましたが、何も機能していないようです。

    first try:
<br/>
<img alt="" ng-style="{'background-image':'url(\'https://www.google.com/images/srpr/logo4w.png\')'}">
<br/>
second try:    
<br/>
<img src="" alt="" ng-style="{'background-image':'url(\'https://www.google.com/images/srpr/logo4w.png\')'}">
<br/>
third try:    
<br/>
<img src="" alt="" ng-style="{background-image:'url(https://www.google.com/images/srpr/logo4w.png)'}">
<br/>
fourth try:    
<br/>
<img src="" alt="" ng-style="{background:'url(https://www.google.com/images/srpr/logo4w.png)'}">

ここにJSFiddleがあります

4

1 に答える 1

1

background-image要素では意味がないので、代わりに<img>を使用する必要があります。ng-srcng-style

また、例では属性が欠落しているng-appため、Angular は実際には実行されていません。

したがって、実際の例は次のようになります。

<div ng-app>
    <img ng-src="https://www.google.com/images/srpr/logo4w.png">
</div>

ng-styleただし、のような他の要素で使用できると思いますdiv。背景が実際に表示されるように、コンテンツが含まれていることを確認してください。

<div ng-app>
    <div style="padding: 20px;" ng-style="{'background-image':'url(\'https://www.google.com/images/srpr/logo4w.png\')'}">This is some content</div>
</div>
于 2014-03-09T10:30:00.860 に答える