2

I want to change the background color of Div with Directive.Here is my code: .

.directive('helloWorld', function() {
  return {
    restrict: 'AE',
    replace: true,
    template: '<p style="background-color:{{color}}">Hello World',
    link: function(scope, elem, attrs) {
              elem.bind('click', function() {
              elem.html("<p>This is the new content</p>");
              elem.css("background-color", "black");
              elem.css("color", "white");
              elem.css("border", "5px solid red");
          });
      elem.bind('mouseover', function() {
      elem.css('cursor', ' ');
      });
    }
  };
});

1 Answer 1

3

This works:

AngularApp.directive('ngToBlack', function() {
    return {
        restrict: 'AE',
        replace: true,
        link: function(scope, elem, attrs) {
            elem.bind('click', function() {
                elem.css("background-color", "black");
                elem.css("color", "white");
                elem.css("border", "5px solid red");
            });
            elem.bind('mouseover', function() {
                elem.css('cursor', ' ');
            });
        }
    };
});

call it as:

<h3 ng-to-black>Change background, color and border</h3>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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