Comments (14)
Maybe it should have a default;
setClassList() {return this.colour?
md-${this.color}:'md-white';}
from components.
My thought is to not include the class attribute if color is undefined, but I'm not sure how to modify the "host" metadata property in the component. Still learning angular2 to see how to get the component to not render with the class attribute if color is undefined. Do you guys know how?
from components.
If returning an empty string still adds 'class' it could be an issue with Angular itself.
from components.
If both
setClassList() {return this.color ? `md-${this.color}` : '';}
and
setClassList() {return this.color ? `md-${this.color}` : null;}
result in
<button md-button ... class>...</button>
I'd consider this an Angular bug.
from components.
I've made a workaround for this. I'm not sure if it's good practice.
Instead binding to the class property, I'm injecting ElementRef into the constructor.
Then I let the component listen to Input() with getter and setter for color.
color_: string;
@Input()
set color(value) {
this.color_ = value;
if (this.elementRef !== undefined) {
this.elementRef.nativeElement.classList.add('md-' + this.color_);
}
}
get color(): string {
return this.color_;
}
constructor(public elementRef: ElementRef) {}
This way no class attribute is rendered on the DOM, when there's no color property provided.
What do you think?
from components.
I even noticed now that when adding an extra class to a md-button, this class is overriden without the fix.
ie:
<button md-button class="hamburger-button"></button>
OR
<button md-button class="hamburger-button" color="accent"></button>
results both in
<button md-button class="md-undefined"></button>
expected:
<button md-button class="hamburger-button"></button>
<button md-button class="hamburger-button md-accent"></button>
from components.
AFAIK using nativeElement
this way is discouraged
this.elementRef.nativeElement.classList.add('md-' + this.color_);
}
as it prevents the code to run in webworker
from components.
If binding to class doesn't work as expected I'd use ngClass
instead.
from components.
@zoechi
Can you show how? I can't get ngClass to work on 'host'.
from components.
Sorry, forgot this is about host. ngClass
can't be used for this.
from components.
So, what are the other options to prevent the problems stated above?
from components.
I'd create a bug report for the behavior where class
is left standing when an empty string is returned.
from components.
Thanks to @zoechi at Stack Overflow I believe we've found a solution for two issues related to this one.
<button md-button></button>
results in<button md-button class="md-undefined"></button>
<button md-button color="accent" class="myCustomClass"></button>
results in ```
Drop [class] on host and add following code might be a solution:
private color_: string;
// @see http://stackoverflow.com/questions/35506395/looking-for-nativeelement-classlist-add-alternative/35514006
@Input()
set color(value: string) {
this.color_ = value;
this.renderer.setElementClass(this.elementRef, 'md-' + this.color_, true);
}
get color(): string {
return this.color_;
}
constructor(private elementRef: ElementRef, private renderer: Renderer) {
}
from components.
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.
from components.
Related Issues (20)
- feat(slide-toggle): prevent value change HOT 1
- feat(cdk/dialog): Support for Leaving Animation - wait for all child animations to complete before disposing the overlay
- Add Styling tab to component pages
- bug(CdkDrag): Breaking drag behavior when injecting CdkDrag directive into a component class. HOT 4
- bug(icon-button): surface color variant not producing any tokens
- docs-bug(multiple): `errors` seems a better choice in templates instead of `hasError()` HOT 1
- bug(sidenav): drawer width is fubar'd
- bug(ALL): The feedback you will ignore; css class names inconsistency in new material - post 15 HOT 2
- bug(MatAutocomplete): Errors not displaying HOT 1
- bug(matTooltip): Tooltip not displaying when mat-icon is nested within mat-button HOT 2
- bug(CdkDragDrop): preview position is incorrect when inside of a popover using translate HOT 6
- bug(mat-datepicker): datepicker color variants not being applied HOT 2
- bug(ThemePalette): ThemePalette for Material 3
- feat(MatTable & MatSort): Ability to track sorting events inside wrapper table component
- bug(SELECT): Chrome crashing on select drop down open HOT 2
- bug(mat-select): mat-select multiple crashes Chrome HOT 30
- feat(snack-bar): Close Icon
- Inputfeat(Input - Textarea): Let the area grow with flexbox
- bug(material/slide-toggle): Animation duration in M3 should be 300ms HOT 1
- bug(cdk/drag-drop): Wrong direction of drop placeholder when moving between two different horizontal drop lists
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from components.