8

This app takes an image, which is essential a background, or workspace. I need to add input text fields onto this workspace given specific coordinates (top, left, width, height). These coordinates would be relative to the image (top/left = 0/0). How would I position my fields/elements relative to the image?

        <img id="background" ng-src="{{page.backgroundImage}}" />
        <input type="text" ng-repeat="field in page.fields" ng-model="field.value" 
               ng-style="{position:'absolute',
                              left:field.left,
                               top:field.top,
                             width:field.width,
                            height:field.height}"/>

The code above works great for absolute positioning but it is not relative to the image.

6
  • You can use ng-class to dynamically add/remove classes and there's also ng-style if you want to directly bind to style values Commented Apr 8, 2014 at 15:48
  • This works great, I still have the problem of how to set my position relative to the other element though. Commented Apr 8, 2014 at 16:12
  • Can't you just wrap both the img and input's list into a div with position relative? Then the absolute position of the inputs would be based upon the div? Commented Apr 8, 2014 at 16:25
  • I added the code for the entire page so you can see the context. Wrapping it in a relative position div isn't doing anything. The image is in the right place, the input box it showing up outside of the image. Commented Apr 8, 2014 at 17:01
  • The image should fill the div, and the inputs would be relative to the div therefore relative to the image. If you're going to solve it just with CSS, I'd suggest working on it with static content (no angular) then plug in the ng-style values from your model. Also an example via fiddle or plnkr would be helpful. Commented Apr 8, 2014 at 18:03

1 Answer 1

4
<div style="position:relative">
    <img id="background" ng-src="{{page.backgroundImage}}" class="course-image" />
    <input type="text" ng-repeat="field in page.fields" ng-model="field.value"
           style="position:absolute"
           ng-style="{left:field.left,
                       top:field.top,
                     width:field.width,
                    height:field.height}" />       
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Just a little heads-up: It is better not to include contant values in ngStyle directive (e.g. position: 'absolute'). This causes an extra evaluation per digest loop (and there are several of those going on all the time). Since it will always evaluate to the same value, just hard-code it in the style attribute.
Just a point in your answer. The attribute style need to be a common html style like: style="position: absolute"

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.