-
Notifications
You must be signed in to change notification settings - Fork 877
bad ngStyle
example
#3072
Comments
That's not the case here ? What are you thinking about ? |
I mean that demonstrating binding to methods in docs without special mention of the pitfalls is a bit dangerous because people might not know about the pitfalls. |
Do you really think someone is going to treat this as a recommended practice. The point is to show how the I'd happily look at a PR. I'm worried that, in making it "correct", we'll lose sight of the point. If you can keep it simple, I'm interested. |
That's why I created this issue, because someone was arguing on SO that this should work because it is even explained this way at (with the link in my original issue comment). Changing it to styles = {
// CSS property names
'font-style': this.canSave ? 'italic' : 'normal', // italic
'font-weight': !this.isUnchanged ? 'bold' : 'normal', // normal
'font-size': this.isSpecial ? '24px' : '8px', // 24px
};
setStyles() {
return styles;
} would already fix the main issue but then styles = {
// CSS property names
'font-style': this.canSave ? 'italic' : 'normal', // italic
'font-weight': !this.isUnchanged ? 'bold' : 'normal', // normal
'font-size': this.isSpecial ? '24px' : '8px', // 24px
}; with <div [ngStyle]="styles"> would also do. I'm not sure what of this is part of the point that is supposed to be made by this example. |
Well, here I am. Whole discussion started from stackoverflow question From docs:
As you see, its not just
Its actually proposed as preferred option without explaining pitfalls. |
Ok well, I prefer the second option styles = {
// CSS property names
'font-style': this.canSave ? 'italic' : 'normal', // italic
'font-weight': !this.isUnchanged ? 'bold' : 'normal', // normal
'font-size': this.isSpecial ? '24px' : '8px', // 24px
}; with <div [ngStyle]="styles"> We can understand how NgStyle directive works syntactically. |
Ya all nailed me for being a grumpy old man. I'll get out a PR. Thanks for making the case. |
No worries 😄, ❤️ your work! |
closes angular#3072, whose criticism prompted these changes.
Fixing in PR #3076 |
…ble (angular#3076) closes angular#3072, whose criticism prompted these changes.
https://angular.io/docs/ts/latest/guide/template-syntax.html#!#ngStyle
should IMHO be considered as DON'T example
It binds a method in the view
[ngClass]="setClasses()"
which is discouraged and should be avoided except one knows what she's doingIt returns a new instance for every call. If it would at least cache the result in an instance field and return that as long as not dependencies are changed, it would be ok.
The text was updated successfully, but these errors were encountered: