简体   繁体   English

具有NgStyle和属性Binding(或其他)的角度变换数据

[英]Angular transform data with NgStyle and property Binding (or sth else)

im learning programming in Angular and i have a problem with property Bindings. 我在Angular中学习编程,我对属性绑定有疑问。 But first the part 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>

The data property contains an Array of Objects and i want to list every Object with his name, id and rev. data属性包含一个对象数组,我想列出每个对象及其名称,id和rev。 I can display them, but now i also want them to rotate dynamically. 我可以显示它们,但现在我也希望它们动态旋转。 So i created a new Array of numbers, which is coming from an function called createNumberArray(). 因此,我创建了一个新的数字数组,该数组来自名为createNumberArray()的函数。 And these numbers shall tell the ngStyle for each loop run which number he shall take to rotate. 这些数字将告诉ngStyle每次循环运行将旋转哪个数字。 This is how i wanted to be it: 这就是我想要的样子:

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

But thats not working cause i can´t get it managed to work, i tryed it with with property binding, but never got the number out of the array inside the transform. 但这不能正常工作,因为我无法使其正常工作,我尝试通过属性绑定对其进行尝试,但从未将数字从变换内部的数组中取出。 And thats the code how it would work (with just the same number all the time): 这就是代码的工作方式(始终使用相同的数字):

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

I managed to get this work with Js, Jquery and html but now i want to do it with Angular. 我设法用Js,Jquery和html来完成这项工作,但现在我想用Angular来做。 And also without Jquery, so i dont just copy past my old code. 而且也没有Jquery,所以我不只是复制过去的旧代码。 But im a bit confused about if i should use Jquery in Angular in the future. 但是我对将来是否应该在Angular中使用Jquery感到有些困惑。 Whats the best way to manupulate the DOM, what do you guys think? 操纵DOM的最佳方法是什么,你们怎么看?

I would really appreciate your help. 我将衷心感谢您的帮助。 Im already thankful that you took the time to read my question. 我已经很感谢您花时间阅读我的问题。

First, set the array values in constructor: 首先,在构造函数中设置数组值:

Typescript: 打字稿:

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

  getNumberArray(index): number {
    return this.anyArrayOfNumbers[index];
  }

Then call getNumberArray from html the method: 然后从html方法调用getNumberArray:

HTML HTML

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

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM