5

I have two ng-style objects in my controller.

$scope.leftpadding = {
    paddingLeft : '7px'
}

$scope.toppadding = {
    paddingTop : '7px'
}

I want to use both these objects in ng-style like

<h5 ng-style="toppadding leftpadding"> 

Is not working. as neither of the objects are being applied in style.

It can't be like

$scope.style = {
  paddingLeft : '7px',
  paddingTop : '7px'
}

Is there any way to achieve this?

2 Answers 2

3

Create a common method in scope

$scope.margeStyleObj = function(objectList) {
    var obj = {};
    objectList.forEach(function(x) {
      for (var i in x)
        obj[i] = x[i];
    });
    return obj;
  }

Then call from html

<h5 ng-style="margeStyleObj([toppadding,leftpadding])"> 

JSFIDDLE

You can also use angular.extend

Like this

  $scope.margeStyleObj = function(objectList) {
    var obj = {};
    objectList.forEach(function(x) {
        angular.extend(obj,x);
    });
    return obj;
  }

JSFIDDLE

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

Comments

2

I am not sure you can use ngStyle in that way. Try this:

ng-style="{'padding-top': paddingTop, 'padding-bottom': paddingBottom}" 

And have $scope.paddingTop to have whatever value you want for padding-top, padding-bottom.

9 Comments

This will make my code huge. It is just an example I put in the question, it may potentially have 5-6 styles for an element.
yeah i understand that, let me dig in a little.
First object is applied, second is not
do you have bottompadding defined in the controller, in your question it is left padding...
@AdityaPonkshe, doesn't look like it. They have the array support for ngClass but not ngStyle.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.