1

I want to set width dynamically. I have tried:

iElement.find("#mycontent").css({width:iElement.width()+ 'px !important'});

In html:

<div id="mycontent"></div>

I am trying above syntax to get width on the div's id "mycontent". When I alert IElement.width(), i have the exact value. But when i add the above code, width is not setting on element or anywhere. I dont understand, why it is not working.

Please help.

1 Answer 1

3

You can do it with angular model.

<div id="mycontent" style="width:{{myWidth}}px; height:400px; background-color:black"></div>

And now you can adjust the width by varying the model $scope.myWidth.

Or you can simply adjust it from the html page itself.

<input type="text" ng-model="myWidth" />

Edit

And also you can set ng-style directive to set width dynamically.

<div ng-style="myStyle" >

And now you can set your width in the model myStyle.

For example, you can set it's width on a button click,

<button ng-click="myStyle = {'width' : '100px'}"> Set a width </button>

Hope this will help you.

Please feel free to ask any doubts about this as comment.

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

12 Comments

It's better to use ng-style instead.
Thank you Remco, Actually I am not aware of ng-style. I am a beginner. Thanks once again to correct me and guide me to correct answer.
@Abhilash P A : I set the scope of myWidth in js file. It is ng-style="width:px;
@prashanth, how do you set your model? $scope.myWidth = 100;?
@Abhilash P A I get it from directive.js file. var hasContent = iElement.find("#mycontent"); if (hasContent) { scope.myWidth = iElement.width(); alert("width: " + iElement.width()); alert("scope width " + scope.myWidth); } Alert has exact values.
|

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.