programing

angularjs ng-style : 배경 이미지가 작동하지 않습니다.

nasanasas 2020. 10. 15. 07:55
반응형

angularjs ng-style : 배경 이미지가 작동하지 않습니다.


각도를 사용하여 div에 배경 이미지를 적용하려고하는데 ng-style(같은 동작으로 이전에 사용자 지정 지시문을 시도했습니다) 작동하지 않는 것 같습니다.

<nav
    class="navigation-grid-container"
    data-ng-class="{ bigger: isNavActive == true }"
    data-ng-controller="NavigationCtrl"
    data-ng-mouseenter="isNavActive = true; $parent.isNavActive = true"
    data-ng-mouseleave="isNavActive = false; $parent.isNavActive = false"
    data-ng-show="$parent.navInvisible == false"
    data-ng-animate="'fade'"
    ng-cloak>

    <ul class="navigation-container unstyled clearfix">
        <li
            class="navigation-item-container"
            data-ng-repeat="(key, item) in navigation"
            data-ng-class="{ small: $parent.isNavActive, big: isActive == true }"
            data-ng-mouseenter="isActive = true; isInactive= false"
            data-ng-mouseleave="isActive = false; isInactive= true">

            <div data-ng-switch on="item.id">

                <div class="navigation-item-default" data-ng-switch-when="scandinavia">
                    <a class="navigation-item-overlay" data-ng-href="{{item.id}}">
                        <div class="navigation-item-teaser">
                            <span class="navigation-item-teaser-text" data-ng-class="{ small: isScandinavia }">{{item.teaser}}</span>
                        </div>
                    </a>
                    <span class="navigation-item-background" data-ng-style="{ 'background-image': 'public/img/products/{{item.id}}/detail/wide/{{item.images.detail.wide[0]}}' }"></span>
                </div>

                <div class="navigation-item-last" data-ng-switch-when="static">
                    <div class="navigation-item-overlay">
                        <div class="navigation-item-teaser">
                            <span class="navigation-item-teaser-text">
                                <img data-ng-src="{{item.teaser}}">
                            </span>
                        </div>
                    </div>
                    <span class="navigation-item-background">
                        <img class="logo" data-ng-src="{{item.images.logo}}">
                    </span>
                </div>

                <div class="navigation-item-default" data-ng-switch-default>
                    <a class="navigation-item-overlay" data-ng-href="{{item.id}}">
                        <div class="navigation-item-teaser">
                            <span class="navigation-item-teaser-text">{{item.teaser}}</span>
                        </div>
                    </a>
                    <span class="navigation-item-background" data-ng-style="{ 'background-image': 'public/img/products/{{item.id}}/detail/wide/{{item.images.detail.wide[0]}}' }"></span>
                </div>

            </div>
        </li>
    </ul>
</nav>

그래도 배경색을 시도하면 잘 작동하는 것 같습니다. 원격 소스와 로컬 소스도 시도했지만 http://lorempixel.com/g/400/200/sports/둘 다 작동하지 않았습니다.


올바른 구문 background-image은 다음과 같습니다.

background-image: url("path_to_image");

ng-style의 올바른 구문은 다음과 같습니다.

ng-style="{'background-image':'url(https://www.google.com/images/srpr/logo4w.png)'}">

몇 가지 방법으로 동적 값을 구문 분석 할 수 있습니다.

이중 중괄호를 사용한 보간 :

ng-style="{'background-image':'url({{myBackgroundUrl}})'}"

문자열 연결 :

ng-style="{'background-image': 'url(' + myBackgroundUrl + ')'}"

ES6 템플릿 리터럴 :

ng-style="{'background-image': `url(${myBackgroundUrl})`}"

데이터가있는 경우 서버가 반환 (item.id)하기를 기다리고 있으며 다음과 같은 구조가 있습니다.

ng-style="{'background-image':'url(https://www.myImageplusitsid/{{item.id}})'}"

다음과 같은 것을 추가하십시오. ng-if="item.id"

그렇지 않으면 두 개의 요청이 있거나 하나의 결함이 있습니다.


For those who are struggling to get this working with IE11

HTML

<div ng-style="getBackgroundStyle(imagepath)"></div>

JS

$scope.getBackgroundStyle = function(imagepath){
    return {
        'background-image':'url(' + imagepath + ')'
    }
}

This worked for me, curly braces are not required.

ng-style="{'background-image':'url(../../../app/img/notification/'+notification.icon+'.png)'}"

notification.icon here is scope variable.


If we have a dynamic value that needs to go in a css background or background-image attribute, it can be just a bit more tricky to specify.

Let’s say we have a getImage() function in our controller. This function returns a string formatted similar to this: url(icons/pen.png). If we do, the ngStyle declaration is specified the exact same way as before:

ng-style="{ 'background-image': getImage() }"

Make sure to put quotes around the background-image key name. Remember, this must be formatted as a valid Javascript object key.


Just for the records you can also define your object in the controller like this:

this.styleDiv = {color: '', backgroundColor:'', backgroundImage : '' };

and then you can define a function to change the property of the object directly:

this.changeBackgroundImage = function (){
        this.styleDiv.backgroundImage = 'url('+this.backgroundImage+')';
    }

Doing it in that way you can modify dinamicaly your style.


The syntax is changed for Angular:

[ngStyle]="{'background-image': 'url(path)'}"

참고URL : https://stackoverflow.com/questions/17252546/angularjs-ng-style-background-image-isnt-working

반응형