1

I have this jsfiddle.

https://jsfiddle.net/helpme128/rba7zqzn/

Inside the jsfiddle, the CSS looks like this;

#container {
   width : auto;
   height: 1200px;
   background:url('https://i.sstatic.net/GgZvy.jpg');
   background-repeat:no-repeat;
}

I would like to modify this jsfiddle such that the css can look more angular like;

#container {
   width : auto;
   height: 1200px;
    background:url({{backgroundURL}});
   background-repeat:no-repeat;
}

Inside the controller, there would be a line like this;

$scope.backgroundURL = 'https://i.sstatic.net/GgZvy.jpg';

How can this be done with angularjs inside css?

5
  • 2
    Hey, this might help: stackoverflow.com/questions/18248437/… Commented Oct 7, 2015 at 10:15
  • 1
    how about instead of using that you just add an attribute class or id. that calls for the background. Commented Oct 7, 2015 at 10:16
  • @ShiguriAnemone', could you give some code example? Sorry, I don't quite follow. Commented Oct 7, 2015 at 10:17
  • 1
    you could target the elem.style.background directly Commented Oct 7, 2015 at 10:21
  • @maioman, sorry, I am new to web apis. would you mind providing some code sample? Commented Oct 7, 2015 at 10:34

4 Answers 4

2

something like this.

<div class="ng-class: expression;"></div>

here's a link for you to check: https://docs.angularjs.org/api/ng/directive/ngClass

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

Comments

1

You can just use the style attribute as following:

<div ng-app="test" ng-controller="testCtrl">
    <div id="container" style="background: url({{backgroundUrl}})">
        <div class="shape" ng-draggable='dragOptions' ></div>
    </div>
</div>

and set the value in your controller as following:

angular.module('test').controller('testCtrl', function($scope) { 
    $scope.backgroundUrl = 'https://i.sstatic.net/GgZvy.jpg';
 })

Check the fiddle link here https://jsfiddle.net/rba7zqzn/5/

Comments

1

This will do the trick:

HTML:

<div ng-app="myApp" ng-controller="myCtrl">
  <div class="{{backgroundURL}}">
    <p>asdfasdfasdf</p>
  </div>
</div>

CSS:

.mybg {
    background: url('https://i.sstatic.net/GgZvy.jpg');
  }

Controller:

angular.module('myApp', [])
  .controller('myCtrl', ['$scope', function($scope) {
    $scope.backgroundURL = "mybg";
  }]);

1 Comment

Sorry, what I want is to have {{ }} inside the css and the contents of {{ }} taken from the controller.
1

As ng-src is angular's in-built directive, you can use a custom directive which provides background for the div as below , moreover with isolate scope u can reuse the directive wherever required.

controller

 $scope.backgroundURL = 'https://i.sstatic.net/GgZvy.jpg';

template

<div background-url="backgroundURL"></div>

directive

     app.directive('backgroundUrl',function(){
          return{
              restrict:"A",// used as attribute in template
              link :function(scope,element,attrs){
                    var backgroundurl = attrs.backgroundUrl;
                    element.css('background','url(' + backgroundurl +')');
                }
          }
      })

Comments

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.