0

I try to apply a css value (itemPosition) for each iteration of my ng-repeat. I call a function that returns me a value (0 to 100). I applied to the css property left.

For example, I have the following values: 53.345, 64.982 or 84.234

All left css properties are set to: 84.234. While I would have:

iteration 1, left: 53345; iteration 2, left: 64982; iteration 3, left: 84234;

My view :

 <div ng-repeat="item in items" ng-init="setItemPosition(item.x)" >
      <div class="circle" style="position: relative; left: {{itemPosition}}%;" >
           <div class="circle-text">{{item.value}}</div>
      </div>
 </div>

In my controller, i have this function (The function returns a percentage) :

 $scope.setItemPosition = function(value) {
    var diffDate = ($scope.intervalResearchTime.endDateTime - $scope.intervalResearchTime.startDateTime) / 100;
    var valueToPercent = ((value - $scope.intervalResearchTime.startDateTime) / diffDate);
    $scope.itemPosition = valueToPercent; //exemple = 84.234;
};

It's possible to do something ?

1
  • 1
    Use the tag ng-style instead of style to execute Angular Expressions within your in-line style. Commented Apr 15, 2014 at 14:07

2 Answers 2

1

You have to use ng-style with function

View

<div ng-repeat="item in items">
  <div class="circle" ng-style="getStyle(item.x)">
       <div class="circle-text">{{item.value}}</div>
  </div>
 </div>

Controller

 $scope.getStyle = function(value) {
    var diffDate = ($scope.intervalResearchTime.endDateTime - $scope.intervalResearchTime.startDateTime) / 100;
    var valueToPercent = ((value - $scope.intervalResearchTime.startDateTime) / diffDate);
    return { left: valueToPercent + 'px' } ;
};

CSS

 .circle {
    position: relative;
 }

Check out jsfiddle http://jsfiddle.net/fizerkhan/eXL28/3/

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

6 Comments

Now all my values ​​are located at 0 percent. why ? Do you have an idea?
Sorry, i dont understand.
When i do, <div class="circle" ng-style="getStyle(item.x)"> it doesn't work. It is all the time at position 0%, as if the css style does not apply. However, if I set in my html page <div class="circle" style="left: 80%;"> : It Works!. Why? Do you have an idea?
Did you check the return values of getStyle()? Debug or put console.log before returning result. I have added semicolons at the end in the return style.
I made a mistake, the return result of ng-style function should be object, NOT a string. I updated the answer.
|
0

you can render data-attr in the html and set a css property: left: attr(name_of_attribute);

I.e:

<div class='circle' data-position='{{itemPosition}}%'></div>


.circle {
  left: attr(data-position);
}

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.