Utilizing NgStyle and Property Binding in Angular to Manipulate Data (or a Different Approach)

Currently, I am diving into Angular programming and encountering an issue with property bindings. Let me share a snippet of my code:

// TS FILE

data[
{"id": "1", "name": "tester", "rev": "1"},
{"id": "2", "name": "tester", "rev": "1"},
{"id": "3", "name": "tester", "rev": "1"},
];


createNumberArray(): number[] {
// code to fill array
return anyArrayOfNumbers
}
<div  *ngFor="let datas of data; let i = index" >


    <ul>
  <li class='list-item' [id]= datas.id [ngStyle]="{ 'transform': 'rotate( numberArray[i] deg) translate(0,-100px) rotate(-numberArray[i]deg)' }">
     <!-- how it would work: <li class='list-item' [id]= datas.id [ngStyle]="{ 'transform': 'rotate(200deg) translate(0,-100px) rotate(-150deg)' }"> -->
            <div class='label1'><img alt='notfound' class='img2' src='any icon'></div>
            <div class='label2'>
                {{ datas.name }}
            </div>
            <div class='label3'>
                {{ datas.id }}
            </div>
            <div class='label4'>
                {{ datas.rev }}
            </div>
        </li>
    </ul>
</div>

I have an Array of Objects in the data property that contains name, id, and rev for each object. The goal is to list these objects while also dynamically rotating them. To achieve this, I created another Array of numbers using the createNumberArray() function. These numbers are meant to dictate the rotation values for ngStyle during each loop iteration:

<li class='list-item' [id]= datas.id [ngStyle]="{ 'transform': 'rotate( numberArray[i] deg) translate(0,-100px) rotate(-numberArray[i]deg)' }">

However, I'm facing difficulties in making this work as intended. I've tried property binding but couldn't extract the number from the array within the transform property. In contrast, here's how it works with a static number value:

<!-- how it would work: <li class='list-item' [id]= datas.id [ngStyle]="{ 'transform': 'rotate(200deg) translate(0,-100px) rotate(-150deg)' }"> -->

In the past, I successfully achieved this using Js, Jquery, and HTML. However, I want to implement this functionality in Angular without relying on Jquery to avoid copying old code. I'm unsure about incorporating Jquery in Angular going forward. What is the optimal approach to manipulate the DOM? Your insights on this matter would be greatly appreciated!

Your assistance means a lot to me, and thank you for investing time in understanding my query.

Answer №1

To start, initialize the array values within the constructor:

Typescript:

  ...
  arrayOfNumbers: number[] = [];
  constructor() {
    for (let i = 0; i < this.data.length; i++) {
      this.arrayOfNumbers.push(200);
    }
  }

  getNumberAtIndex(index): number {
    return this.arrayOfNumbers[index];
  }

Next, invoke getNumberAtIndex method from the HTML:

HTML

...[ngStyle]="{ 'transform': 'rotate('+numberArray[i]+'deg) translate(0,-100px) rotate('+numberArray[i]+'deg)' } ...

DEMO

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

Arrange the Elements of Select Options

I am currently facing an issue with organizing the elements in my select list. I have two interconnected lists, one containing a list of "models" which I have successfully sorted using the following function: var sel = $('#model'); var opts_lis ...

JavaScript module dependency management technique

Just starting out with JavaScript, I'm attempting to incorporate a JavaScript library/sdk. I've familiarized myself with common patterns and currently considering using the revealing module pattern. To provide a concrete example, let's imagi ...

When attempting to retrieve information, the variable is displaying as undefined, although the console log is indicating that data

Here is the code I am currently using: function get_last_chat_time(steamid){ if(!db_connect) { return 0; } chat_db.aggregate( [ {"$match":{"name":"chat-msg",steamid: steamid}}, { "$sort" : { date : -1 } }, {"$limit" : 1} ...

Identify the difference between a regular function and a constructor in JavaScript

How can we determine whether a function in javascript is a simple, plain (anonymous) function, or a constructor (a function with a prototype)? I have developed the following function for this purpose: function checkFunctionType(value) { var ownPropert ...

Exploring Jasmine's callThrough and callFake capabilities

One of the methods in my codebase returns function references based on different cases. function methodToBeMocked(param){ case 1: return func1; case 2: return func2; . . case n: return funcN; } Now, I need to spy on this method and provide a fake ...

Incorporating a new textfield in Codeigniter through a button/link activation

Currently, I am working on designing a request form for my website. I am facing an issue with creating a button that can dynamically add new input fields when clicked. Unfortunately, I am unsure of how to resolve this problem. Picture this: [ button ] A ...

Implementing dual language codes for a single locale in internationalization (i18n) efforts

I am currently using i18n to display translations in English and Japanese. The default language is set to English with the code en, but I have recently discovered that my website is not utilizing the correct ISO language code for Japanese, which should be ...

Dividing ReactJS into different assemblies

I have a flexible .NET application that utilizes plugins to load controllers, views, and components from different assemblies. I am interested in learning React (completely new to me) and I have a question. Can React split its components into separate as ...

Create a visually appealing DataGrid cell by incorporating a vibrant colored oval with text or an icon inside

I'm in need of assistance with the mui/x-data-grid plugin. My goal is to display a cell within the grid containing both text and an icon, based on the value of the text. Unfortunately, I have been unable to achieve this so far. Here is the link to the ...

Exploring the capabilities of Express.JS for integrating with an external API

const express = require('express'); const app = express(); const path = require('path'); const api = require('./api'); app.get('/', function(req, res){ res.sendFile(path.join(__dirname + '/index.html')); ...

The $scope within the $ionicplatform is not functioning as expected

I've been working on an application, but it doesn't seem to be functioning correctly. Typically, when we add a value to the scope, I expect it to update in the application. Here is the snippet from index.html: <body ng-app="starter"> <i ...

What methods can I use to visually distinguish external links from those on my website?

Can CSS be used to show users which links are external? ...

Using CSS to align the second line of a label directly beneath the first line through indentation

I need to adjust the second line of the label to ensure it doesn't overlap with the starting point of the first line. Below is my HTML code: <label for="ui-multiselect-edit-1" title="" class="ui-corner-all ui-state-hover"> <inpu ...

What is the best way to utilize *ngSwitchWhen in a TypeScript environment?

I am currently working with Ionic2 and Angular2 and encountering an issue while trying to implement a segment using ngSwitchWhen. Unfortunately, the functionality is not working as expected and I am receiving an error message. How can I resolve this issue ...

Changes made in React are not reflected in the DOM

import React, { Component } from "react"; import ReactDOM from "react-dom"; import "./index.css"; class App extends Component { constructor(props) { super(props); this.state = { text: "", listItem: [] } this.onChangeInpu ...

How to use jQuery to target every anchor element within an unordered list

I am trying to manipulate an unordered list structure by changing the class of the anchor tags when a specific link is clicked. For example, when the link in List Item 1 is clicked, I want to change the class of all links underneath List Item 1 to "yes". ...

How to position a Bootstrap 4 button group at the bottom of a flexbox container

I am working with HTML code that looks like this: <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/> <div class="border d-flex align-items-baseline m-2 p-2" style="width: 400px"> < ...

Load annotations from a JSON file with Annotator.js

Seeking assistance with incorporating annotations into my website using annotator.js. I have been encountering difficulties getting it up and running successfully. My goal is to display highlighted annotations upon page load, but I keep receiving a JavaSc ...

Searching for two distinct nested key values in Ramda

I am new to Ramda and wondering if it is possible to retrieve two different key values at the same level of an object. Below is the code I have added: In this scenario, the object 'list' contains keywords 'users' and 'employee&ap ...

When utilizing Ionic Firebase, the user profile saved in the sidemenu is stored using the Events class. However, the saved profile disappears as soon as

Hello there. I am currently working on displaying my user's information in the sidemenu, and after some research, I found that using the Events class might solve this issue. However, I have noticed that the data saved in subscribe gets destroyed whene ...