-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ng-host #19119
Comments
great idea! Came here to this url because I was thinking of / googling for the same feature. Would give the View some more oomph and make at least event processing a lot easier. |
The main reason I'd like to have this is because I like my html to be easily readable and as lean as possible.
Using |
great @trotyl but how can I use it for structural directives (ex ngIf ) |
@vladuionut No, you can't. There's not something can be done by a proxy renderer. And that's not logical at all, the binding value are coming from component (instance) context, how could the binding work without instantiate component first? |
I am sick of adding the following to my components in order to use the style classes that we use. I would really love an ng-host like option. @HostBinding('class.Flex')
flex = true;
@HostBinding('class.Flex-Direction-Columnn')
flexDir = true;
@HostBinding('class.Flex-1')
flex1 = true; In order to add classes to my component, this is what I have to do. |
I know that this is not exactly what people are asking for here but when ivy lands one will be able to add classes to host in a non-destructive way, ex.: @Component({
selector: '...'
host: { class : 'Flex Flex-Direction-Columnn Flex-1' },
template: `...`
})
class MyComponent {
...
} which is much nicer as compared to : @HostBinding('class.Flex')
flex = true;
@HostBinding('class.Flex-Direction-Columnn')
flexDir = true;
@HostBinding('class.Flex-1')
flex1 = true; |
You can already do that. Or is the "non-destructive" part of the sentence important here ? :p |
"non-destructive" is the key part here - today doing |
@pkozlowski-opensource Would it possible to have a property-way of non-binding non-event host attribute declaration? Even the StyleGuide is recommending always using property decorator for that. EDIT: doesn't seem to be a big issue for @HostBinding('class')
foo = {
'Flex': true,
'Flex-Direction-Column': true,
'Flex-1': true,
} |
I was about to open exactly same feature-request when i found this 7 year old thing. This would be very very useful, especially improving code readability. |
Hey there, thank you for this great proposal.
|
Hi together, here is an approach my team came up with to allow style overrides for a component using Tailwind.
import { Component, computed, input } from '@angular/core';
import { twMerge } from 'tailwind-merge';
@Component({
selector: 'app-label',
standalone: true,
host: {
'[class]': 'hostClass()',
},
template: `<ng-content></ng-content>`,
})
export class LabelComponent {
#classDefaults = 'text-red-500';
protected hostClass = computed(() =>
twMerge(this.#classDefaults, this.class())
);
class = input<string>('');
} <app-label class="text-green-500">Hello, there!</app-label>
<!-- default text color "red" will be overriden by color "green" --> Currently, this serves our needs, but having a mechanism like |
Came to Angular's issues to propose almost exactly the same feature. |
I got Angular project but I'm a guy with React experience. Came here while looking for a way to get rid of those unnecessary extra top-level tags that are breaking my sibling/direct-child CSS selectors. |
Proposal
I'd like an
<ng-host>
that would act in a similar way as the:host
css property. That would apply directives and classes to the host component. Related #8785Current behavior
my-component.template.ts
Currently to apply a class
flex
to the host component one canhost
in the component decorator (or@HostBinding)
;All of which are suboptimal.
1. Host
(Same applies for
@HostBinding()
).Class declarations belongs in my template. Directives don't work here
2. Wrapper
Adds an additional tag.
3. Defining in parent
Leaks implementation outside the component itself.
Expected behavior
result
Compiling wrong usages
1. multiple ng-host
2. Multiple ng-host nested
3. ng-host not used as the topmost component
solution 1
Strictly speaking it would make sens to have those be invalid.
solution 2
ng-host
act as a strict link to the host. Meaning that the above scenarios would result, in all cases, in thisThus when the compiler finds a
ng-host
element it just links whatever it finds to the host. No matter where it's put. I find this a bit less intuitive but could be easier to implement.solution 3
compiles to this
The text was updated successfully, but these errors were encountered: