Alter Elements in AngularJS Based on Their Relationship to Other Elements

Creating diagonal lines that connect squares in the corner using HTML/CSS is my current project. I am currently accomplishing this by transforming divs with a custom Angular directive, but I'm wondering if there's a simpler way to achieve this without relying heavily on JQuery-like techniques since I'm new to Angular.

I need the design to be dynamic so that I can easily add more boxes on each side, resize the existing ones, or incorporate additional content. Therefore, static CSS won't suffice for this task.

Check out my current attempt here, although I'm having trouble aligning the side label in the center and not sure why. Any suggestions would be greatly appreciated!

Thank you!

Answer №1

I updated my custom command to the following: http://plnkr.co/edit/xyOBA6iCx4BPqjqBlPYN?p=preview

Instead of using ids for each table cell, I decided to place them in the first row:

<td id="row-1">1</td>
</tr>
<tr><td id="row-2">2</td></tr>
<tr><td id="row-3">3</td></tr>

I also assigned an id to the first columns:

<td id="col-1" width="34px">1</td>
<td id="col-2" width="34px">2</td>
<td id="col-3" width="34px">3</td>

The new directive is now called "draw-line," which utilizes mathematical calculations for determining the length:

var from_elem = angular.element( document.querySelector( '#' + attrs.from ) );
var to_elem = angular.element( document.querySelector( '#' + attrs.to ) );
var height_distance = from_elem[0].getBoundingClientRect().top - to_elem[0].getBoundingClientRect().top;
var width_distance = from_elem[0].getBoundingClientRect().left - to_elem[0].getBoundingClientRect().left;
var distance = Math.sqrt(
                Math.pow(height_distance, 2) +
                Math.pow(width_distance, 2)
);
var angle = Math.atan(height_distance / width_distance);
var my_current_border = element[0].getBoundingClientRect().width;
var required_padding = (distance - my_current_border) / 2;
element.css({
    'top': to_elem[0].getBoundingClientRect().top + 'px',
    'left': to_elem[0].getBoundingClientRect().left + 'px',
    'border-top': '1px solid',
    'position': 'absolute',
    'padding-left': required_padding + 'px',
    'padding-right': required_padding + 'px',
    'transform': 'rotate(' + angle + 'rad)',
    'transform-origin': 'left top'
});

Currently, it only functions properly for top left to top left connections, but that meets my requirements.

Is it a common practice in Angular to select elements as demonstrated below?

var from_elem = angular.element( document.querySelector( '#' + attrs.to ) );

and subsequently calculate the position like so:

var height_distance = from_elem[0].getBoundingClientRect().top - to_elem[0].getBoundingClientRect().top;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Scrapy Library's Response.css function fails to retrieve data from API, resulting in an empty list

scrapy shell 'https://www.samsung.com/in/smartphones/galaxy-m/' fetch('https://searchapi.samsung.com/v6/front/b2c/product/finder/gpv2?type=01010000&siteCode=in&start=1&num=12&sort=onlineavailability&onlyFilterInfoYN=N& ...

Tips on creating a button that, upon clicking, triggers the download of an Excel file using PHPSpreadsheet

I am trying to figure out how to create a button that, when clicked, will download an Excel file named b1b5.xls with some specified values added. Here is the code I have so far: <html> <head> </head> <body> <center> < ...

Tips for accessing and manipulating the parent, sibling, next, and previous elements of the current element with AngularJS

Below is an example of HTML code where I have linked $scope properties and values to controls: https://i.sstatic.net/NRP0i.png This is the function that triggers when an image is clicked and changes the path of the images: $scope.ratingClick = function( ...

The functionality of `setState` is experiencing issues when used in conjunction with the `onLongPress` event of

I am new to react-native and I am trying to create a feature where a counter increases or decreases when a button is pressed for a long duration. I am using the onLongPress prop of TouchableOpacity along with setTimeout() to delay the state change by 400ms ...

Creating a properly formatted JSON array in PHP

After writing an ajax call to retrieve an array from a .php file (upon clicking btn_getAnswers), everything was running smoothly with integer data sourced from the database. However, as soon as I attempted to return an array filled with three Strings, the ...

Include CLI input into JavaScript variable within the Webpack build process

I am currently attempting to incorporate a variable into my app.js file during the build process. For instance: //app.js var myvar = {{set_from_cli}}; Afterwards, I would like to execute something such as webpack -p --myvar='abc' which would pr ...

Guide on setting a wait while downloading a PDF file with protractor

As I begin the download of a pdf file, I realize that it will take more than 2 minutes to complete. In order to verify if the file has successfully downloaded or not, I will need to wait for the full 2 minutes before performing any verification checks. C ...

What will be the appearance of pixel widths on resolutions higher than?

What happens if I set all widths on my website to 1920px and view it on a higher resolution like 4k? Will it only take up a portion of the screen while leaving white space around it (similar to zooming out)? In essence, can I scale the entire website prop ...

Tips for sending a form as a PDF attachment through email using Nodemailer, React, and Node.js

Imagine a scenario with a React form. Here's what I envision: fill out the form, click the submit button, and watch as the form magically generates a PDF that gets sent as an attachment via Nodemailer. Sure, I've successfully managed to send emai ...

Storing and Retrieving Multiple Data with localStorage

I need assistance with modifying my code. I have an input field labeled "mail" and I am trying to store email addresses and corresponding IDs in local storage. The email address should be taken from the "mail" input field while the ID should increment each ...

Utilizing a try/catch block to handle exceptions arising from unhandled promise re

I decided to intentionally test my async function by querying a table that does not exist. This way, I could generate an error on purpose. async function getPosts() { try { const connection = await dbConnection() const result = await connection. ...

What is the best way to update the text on buttons once they've been clicked for a variety of buttons

I'm encountering an issue with multiple buttons where clicking on any button causes the text to change on the first button, rather than the one I actually clicked on. All buttons have the same id, and using different ids for each button seems impracti ...

Tips for incorporating inline images with gatsby-image

Seeking Solution In my quest to query and showcase images with a maximum width of 350px, I am hoping to have them displayed inline-block for tablets and larger screens. Ideally, each image would sit next to one another and wrap if they exceed the parent d ...

What is the advantage of encapsulating a helper function within an IIFE instead of declaring it directly within the body of the function where it is being

Exploring the functionalities of Babel and ES6, I found myself stuck while transpiling some code at this particular section: class App extends SomeParent { myFunction() { } } I am curious about the output that caught my attention: var _createCl ...

Triggering a postback event with an alert upon confirmation in an ASP.NET button

Encountering an issue with the javascript alert in my ASP.NET application. I have a button that deletes a user from the database. However, users may have associated documents and insurance policies. I want to trigger a javascript alert if the user has an ...

Is there a way to adjust the dimensions of individual pictures?

Dealing with a variety of images within a resizing container can be tricky. I have a solution for adjusting each image to a specific size without affecting the others. Currently, the container's CSS is set to resize images based on browser size: .con ...

What are the steps to implement a Bottom Navigation bar in iOS and a Top Navigation bar in Android using NativeScript Angular?

For my project, I decided to utilize the tns-template-tab-navigation-ng template. I am currently working on creating a WhatsApp clone, and in iOS, it features a bottom navigation bar while in Android, it has a top navigation bar. I am looking for guidance ...

`There seems to be an issue with inserting form data into the database using PHP and MySQL`

I am facing an issue with inserting form data into MySQL. After entering the details and submitting, I am unable to see the data in the database. Can someone help me identify the problem? <?php // PHP error reporting setup ini_set('error_repo ...

I possess an array with a specific structure that I wish to substitute the keys as demonstrated below

The data array is currently structured in this way. I have attempted various methods but have not been able to find a suitable solution. I need to send the JSON below via an AJAX request, however, I do not want the keys 0 and 1 in both child arrays. [sch ...

Tips for adding a class to an SVG when clicked

I want to make an SVG change color to white when clicked and its container (icon_container) to change to blue. How can I achieve this? Below is the code snippet: state = { active: false, }; click = () => { this.setState({active: !this.state.active ...