0

I am a newbie to AngularJS. I developed a simple currency converter app for which I would like to set a nice image background. This is my UI code:

<head>

<title></title>
</head>
<body >
<center>
    <div ng-style="{'background-image':'url(https://static.vecteezy.com/system/resources/previews/000/050/156/original/9-stylish-vector-world-map-vector.jpg)'}">
        <div class="label" ng-controller="countryMapper" style="display:inline-block">

         //Internal code here


        </div>
    </div>
</center>
<script src="Scripts/angular.min.js"></script>
<script src="app/app.module.js"></script>
<script src="app/main.js"></script>

I am just not able to set a full background image to the app. Please tell me how to do this in detail

1) If fetching the image from a URL

2) If using a local image, saved in the project

1
  • you got it sorted? Commented Nov 7, 2016 at 17:50

2 Answers 2

1

The problem appears to be that your code tries to call a controller without first calling a specified module! After defining a module and including it in the source code, the background image displays exactly as it's meant to:

<body ng-app="myapp">
<!-- ngApp needs to be here to use any controllers attached to the module! -->

  <div ng-style="{'background-image':'url(https://static.vecteezy.com/system/resources/previews/000/050/156/original/9-stylish-vector-world-map-vector.jpg)'}">
    <div class="label" ng-controller="countryMapper" style="display:inline-block">

Check out the Plunker here!

Also, just as an aside, make sure your app is tall enough to show the image in the first place; keep in mind that the first bit of the image is just white, so you wouldn't be able to see that it was there at all if the div was only a few lines long!

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the $scope variable bind the image url using the following directive,

    var app = angular.module('myApp', []);
    app.controller("countryMapper", function ($scope) {    
 $scope.url="https://static.vecteezy.com/system/resources/previews/000/050/156/original/9-stylish-vector-world-map-vector.jpg";
    });

Directive

app.directive('backgroundImage', function () {
    return function (scope, element, attrs) {
        element.css({
            'background-image': 'url(' + attrs.backgroundImage + ')',
            'background-repeat': 'no-repeat',
        });
    };
});

DEMO

4 Comments

@SoulRayder check the demo
I tried copy pasting same code into my project, but its not showing the image
can i get team viewer?
I'm afraid I don't have that installed currently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.