0

Here is a clickable div. Once it clicked, it's supposed to change its position (defined by an inline CSS rule). The callback function is successfully invoked.

Problem: model change does not affect the inline CSS. Although initial values works well.

View:

<div data-ng-repeat="item in items"
     data-ng-click="item.clicked()"
     data-ng-style="{'left': '{{item.left}}', 'width': '{{item.width}}', 'top': '0' }">
</div>

Controller:

var item = {
    left: '30%',
    width: '20%'
};

(function(item) { // because inside a loop
    item.clicked = function() {
        console.log("item clicked");
        item.width = '100%';
        item.left = '0%';
    };
})(item);

$scope.items.push(item);

Angular 1.4

0

2 Answers 2

2

You just don't need the extra {{

data-ng-style="{'left': item.left, 'width': item.width, 'top': '0' }"
Sign up to request clarification or add additional context in comments.

Comments

0

As said in the documentation of ng-style

Known Issues: You should not use interpolation in the value of the style attribute, when using the ngStyle directive on the same element. See here for more info.

Example how to use

<input type="button" value="set color" ng-click="myStyle={color:'red'}">
<input type="button" value="set background" ng-click="myStyle={'background-color':'blue'}">
<input type="button" value="clear" ng-click="myStyle={}">
<br/>
<span ng-style="myStyle">Sample Text</span>
<pre>myStyle={{myStyle}}</pre>

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.