I am trying to give my dynamic div style - I want css to come from the controller, where the css object that the controller returns is not hardcoded, but changes dynamically (I need a "top" field, which is a variable). I use ng-style to achieve this:
-html- <div id="preview" ng-style="setTopPosition()"> -controller- $scope.setTopPosition = function() { console.log("top position: " + $scope.topPosition); var retObj = { top : $scope.topPosition , background: "green" }; console.log(retObj); return retObj };
Now I know that the values ββthat I expect ($ scope.topPosition, etc.) are there (they are displayed in the console log), and I know that the controller runs its code, as the background div turns green. However, the top position does not work. Can ng-style use variable field objects? Also, does it need to use a custom directive instead?
source share