1

I've got two select boxes. The first one is changing the entires of the second one. The second one always adjusts it's length to the longest of the actual entries. But it looks wuite ugly if the width always changes when i select another item of the first box.

Is it possible to set the width of the second box to a fixed value, calculated by the longest element of the whole (non-filtered) second box element list? In the scope I can calculate the number of characters of the longest element. Can I adjust this to the width somehow?

I already tried (for testing)

<select ... style="width:{{getMaxLength()}}px" > </select>

returning the number of characters by the method getMaxLength() in the scope. But it didn't change anything.

Did anyone already try this? Setting the size with ng-attr-size worked, but it's the height which does help me nothing.

I'd appreciate every hint or idea! jana


Addition:
My example did not work with the answers because I didn't give the a "name" property. Then the answer from Muhammad Reda worked fine and I'm sure most of the other ideas will work as well. I found out by very thoroughly comparing Muhammads plunker with my code. That was the only difference.

2
  • 1
    Can you create a fiddle of your issue? Commented Nov 17, 2014 at 12:58
  • Sorry, I'm not used to fiddle so far. If there's time, i'll create one. Commented Nov 17, 2014 at 13:30

3 Answers 3

2

You can use ngStyle

From documentation:

The ngStyle directive allows you to set CSS style on an HTML element conditionally.

Example:

<select ng-style="{'width': getMaxLength() + 'px'}"></select>

Plunker

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

6 Comments

The sytax seems to be okay (wasn't marked as a misstake) but it has no effekt. Do I have to install some packages beneath angular main package or something?
Try to print the value of the color inside getMaxLength() to the console.
The value which is calculated by getMaxlentgh() is 500 and it get's printed on the console as well as printed in the html UI when I put it there as plain text with {{getMaxLength()}}. So the method and the value are correct...
Now i tried a fixed String like "width: 500 px" as a scope variable, just like in your plunker. I can print it in the UI, but as ng-style it doesn't have any effect on my UI...
|
1

Use this ng-style="{'width': {{getMaxLength()}} + 'px'}" instead of style element

1 Comment

This syntax seems to be wrong... my editor marked it as a misstake 8and it knows angular), it also didn't help much, see also the answer of Muhammed Reda.
0
ng-style="customStyle" ..

and in your controller,

$scope.customStyle = {'width':$scope.getMaxLength()}

1 Comment

I used this and it seems to have no effect either :-( I use newest version of firefox...

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.