Don't use functions in your template. They are called on every application tick and can run very often depending on your code.
One way to avoid this is to compute the value and assign it to a property in your component and then use that property to bind to ngStyle
. That way the function will only be called once.
this.compStyle = { marginLeft: Math.floor(Math.random() * 83) + 1 + 'vw', marginTop: Math.floor(Math.random() * 145) + 1 + 'vw' }
Template:
[ngStyle]="compStyle"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…