0

Basivally i have a div ,which i'm putting a dynamic style

<div style="position:relative; left: {{et.getNumPadStyle()}}px">

My function getNumPadStyle will have

function getNumPadStyle {
    var num = "25";
    return num;
}

This is giving me an error

Referencing DOM nodes in Angular expressions is disallowed! Expression: et.getNumPadStyle()

And when i inspect the element, the exact number is not reflecting

element {
    position:relative;
    left:{{et.getNumPadStyle()}}px
}

How to resolve this?

3 Answers 3

3

Your angular function has to be in scope and called with $scope.getNumPadStyle()

    $scope.getNumPadStyle = function ()
    {
        var num = "25";
        return num;
    }

See it working here: https://jsfiddle.net/AKMorris/nq19be7h/

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

Comments

2

There are few errors in your code . Find the code example below

Html :

<div ng-app>
  <div ng-controller="TodoCtrl">
  <div style="position:relative; left: {{getNumPadStyle()}}px">test</div>
</div>

JS :

function TodoCtrl($scope) {
    $scope.getNumPadStyle= function() {
    var num = "455";
    return num;
}
  };

2 Comments

I am able to get the number in my view.. The problem is binding.. not the scope for sure
I think the issue is scope for example why do you use 'et' in {{et.getNumPadStyle()}} ? As you can see above @selvassn does not use 'et'...
1

Try with ng-style like this:

<div ng-style="et.getNumPadStyle()">

And in Angular code:

    $scope.et = {};
    $scope.et.getNumPadStyle = {
        "padding-left" : "25px"
    }

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.