Angular @HostBinding, simple example and fundamentals
up vote
1
down vote
favorite
I would understand very well the Angular @HostBinding concept. Unfortunately, my book is very good but this concept is not very clear explained.
See please the code:
@Component(
selector: 'app-test-component',
templateUrl: './test-component.component.html',
styleUrls: ['./test-component.component.css']
)
export class TestComponentComponent implements OnInit {
@Input() dataModel:AppModel;
@HostBinding('attr.class') cssClass = 'alfa';
constructor() {
(...)
My personal explanation: "host binding allow to set something in the host element (in this case the app-test-component tag) from within the component it self (in other words, from this file I mentioned below); in this case, I set the class attribute of this tag to the variable named cssClass and with attribute 'alfa'". Is it correct?
In this case, if I defined the 'alfa' style in the corrispondent css file, why I don't see this style (i.e. background color or something else) in the page which shows my component?
Thank you very much!
EDIT: I need to understand very well the row
@HostBinding('attr.class') cssClass = 'alfa';
Can you please confirm that is exactly equivalent to "set the class attribute of the template element to the string cssClass assigned to value 'alfa'"? (or, in other words, is the same as the instruction "class='alfa'" in the main template tag)
And, can you please write me also an example with the same result but without the using of @hostbinding? I am sure, that see this equivalent two solution in comparision can be very helpful for me.
Thank you very much for your patience!
angular angular2-hostbinding
add a comment |
up vote
1
down vote
favorite
I would understand very well the Angular @HostBinding concept. Unfortunately, my book is very good but this concept is not very clear explained.
See please the code:
@Component(
selector: 'app-test-component',
templateUrl: './test-component.component.html',
styleUrls: ['./test-component.component.css']
)
export class TestComponentComponent implements OnInit {
@Input() dataModel:AppModel;
@HostBinding('attr.class') cssClass = 'alfa';
constructor() {
(...)
My personal explanation: "host binding allow to set something in the host element (in this case the app-test-component tag) from within the component it self (in other words, from this file I mentioned below); in this case, I set the class attribute of this tag to the variable named cssClass and with attribute 'alfa'". Is it correct?
In this case, if I defined the 'alfa' style in the corrispondent css file, why I don't see this style (i.e. background color or something else) in the page which shows my component?
Thank you very much!
EDIT: I need to understand very well the row
@HostBinding('attr.class') cssClass = 'alfa';
Can you please confirm that is exactly equivalent to "set the class attribute of the template element to the string cssClass assigned to value 'alfa'"? (or, in other words, is the same as the instruction "class='alfa'" in the main template tag)
And, can you please write me also an example with the same result but without the using of @hostbinding? I am sure, that see this equivalent two solution in comparision can be very helpful for me.
Thank you very much for your patience!
angular angular2-hostbinding
Thank but I haven't found in this link any answer to my specific question.
– Archimede
Nov 12 at 7:34
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I would understand very well the Angular @HostBinding concept. Unfortunately, my book is very good but this concept is not very clear explained.
See please the code:
@Component(
selector: 'app-test-component',
templateUrl: './test-component.component.html',
styleUrls: ['./test-component.component.css']
)
export class TestComponentComponent implements OnInit {
@Input() dataModel:AppModel;
@HostBinding('attr.class') cssClass = 'alfa';
constructor() {
(...)
My personal explanation: "host binding allow to set something in the host element (in this case the app-test-component tag) from within the component it self (in other words, from this file I mentioned below); in this case, I set the class attribute of this tag to the variable named cssClass and with attribute 'alfa'". Is it correct?
In this case, if I defined the 'alfa' style in the corrispondent css file, why I don't see this style (i.e. background color or something else) in the page which shows my component?
Thank you very much!
EDIT: I need to understand very well the row
@HostBinding('attr.class') cssClass = 'alfa';
Can you please confirm that is exactly equivalent to "set the class attribute of the template element to the string cssClass assigned to value 'alfa'"? (or, in other words, is the same as the instruction "class='alfa'" in the main template tag)
And, can you please write me also an example with the same result but without the using of @hostbinding? I am sure, that see this equivalent two solution in comparision can be very helpful for me.
Thank you very much for your patience!
angular angular2-hostbinding
I would understand very well the Angular @HostBinding concept. Unfortunately, my book is very good but this concept is not very clear explained.
See please the code:
@Component(
selector: 'app-test-component',
templateUrl: './test-component.component.html',
styleUrls: ['./test-component.component.css']
)
export class TestComponentComponent implements OnInit {
@Input() dataModel:AppModel;
@HostBinding('attr.class') cssClass = 'alfa';
constructor() {
(...)
My personal explanation: "host binding allow to set something in the host element (in this case the app-test-component tag) from within the component it self (in other words, from this file I mentioned below); in this case, I set the class attribute of this tag to the variable named cssClass and with attribute 'alfa'". Is it correct?
In this case, if I defined the 'alfa' style in the corrispondent css file, why I don't see this style (i.e. background color or something else) in the page which shows my component?
Thank you very much!
EDIT: I need to understand very well the row
@HostBinding('attr.class') cssClass = 'alfa';
Can you please confirm that is exactly equivalent to "set the class attribute of the template element to the string cssClass assigned to value 'alfa'"? (or, in other words, is the same as the instruction "class='alfa'" in the main template tag)
And, can you please write me also an example with the same result but without the using of @hostbinding? I am sure, that see this equivalent two solution in comparision can be very helpful for me.
Thank you very much for your patience!
angular angular2-hostbinding
angular angular2-hostbinding
edited Nov 12 at 7:58
asked Nov 12 at 7:24
Archimede
102112
102112
Thank but I haven't found in this link any answer to my specific question.
– Archimede
Nov 12 at 7:34
add a comment |
Thank but I haven't found in this link any answer to my specific question.
– Archimede
Nov 12 at 7:34
Thank but I haven't found in this link any answer to my specific question.
– Archimede
Nov 12 at 7:34
Thank but I haven't found in this link any answer to my specific question.
– Archimede
Nov 12 at 7:34
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
In Angular, the @HostBinding() function decorator allows you to set the properties of the host element from the directive class.
Let's say you want to change the style properties such as height, width, color, margin, border, etc., or any other internal properties of the host element in the directive class. Here, you'd need to use the @HostBinding() decorator function to access these properties on the host element and assign a value to it in directive class.
The @HostBinding() decorator takes one parameter, the name of the host element property which value we want to assign in the directive.
Thank you very much. In other words, @HostBinding('someProperty') someVariable = 'someValue'; means: set the property of someProperty (in the tag element in which I have the selector, obviously) to the someVariable, which I have assigned to someValue. I.E.: if I want set backColor of my host element to red, I should write @HostBinding('style.color') color = 'red' or @HostBinding('style.color') color: string; if I want define the color in other points of my class. And, all is referred to the given selector. Correct?
– Archimede
Nov 12 at 9:39
1
Yes, it's correct, here other example: @HostBinding('style.border-color') borderColor: string; and this.borderColor = 'pink';
– ararb78
Nov 12 at 10:46
Perfectly understood.
– Archimede
Nov 12 at 11:27
add a comment |
up vote
1
down vote
The :host() CSS pseudo-class function selects the shadow host of the
shadow DOM containing the CSS it is used inside (so you can select a
custom element from inside its shadow DOM) — but only if the selector
given as the function's parameter matches the shadow host.
Ref:https://developer.mozilla.org/en-US/docs/Web/CSS/:host()
Try this it will work
component.css
:host(.alfa)
color: red;
Sorry, is not what I means. Now, I try to explain more clearly in my original question. I am related to the @HostBinding row, nothing else.
– Archimede
Nov 12 at 7:49
you mean the style not applying or what?
– Chellappan
Nov 12 at 8:39
No, the main goal of my question is not focused on the style but on the HostBinding command. I have edited my original question. Thank you very much for your patience!
– Archimede
Nov 12 at 9:05
Angular use shadow DOM to encapsulate the style.That is why your styles are not getting applied
– Chellappan
Nov 12 at 9:15
Thank but the question is not (not) focused to a css / dom problem but to understanding very well the HostBinding instruction.
– Archimede
Nov 12 at 9:31
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257512%2fangular-hostbinding-simple-example-and-fundamentals%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
In Angular, the @HostBinding() function decorator allows you to set the properties of the host element from the directive class.
Let's say you want to change the style properties such as height, width, color, margin, border, etc., or any other internal properties of the host element in the directive class. Here, you'd need to use the @HostBinding() decorator function to access these properties on the host element and assign a value to it in directive class.
The @HostBinding() decorator takes one parameter, the name of the host element property which value we want to assign in the directive.
Thank you very much. In other words, @HostBinding('someProperty') someVariable = 'someValue'; means: set the property of someProperty (in the tag element in which I have the selector, obviously) to the someVariable, which I have assigned to someValue. I.E.: if I want set backColor of my host element to red, I should write @HostBinding('style.color') color = 'red' or @HostBinding('style.color') color: string; if I want define the color in other points of my class. And, all is referred to the given selector. Correct?
– Archimede
Nov 12 at 9:39
1
Yes, it's correct, here other example: @HostBinding('style.border-color') borderColor: string; and this.borderColor = 'pink';
– ararb78
Nov 12 at 10:46
Perfectly understood.
– Archimede
Nov 12 at 11:27
add a comment |
up vote
1
down vote
accepted
In Angular, the @HostBinding() function decorator allows you to set the properties of the host element from the directive class.
Let's say you want to change the style properties such as height, width, color, margin, border, etc., or any other internal properties of the host element in the directive class. Here, you'd need to use the @HostBinding() decorator function to access these properties on the host element and assign a value to it in directive class.
The @HostBinding() decorator takes one parameter, the name of the host element property which value we want to assign in the directive.
Thank you very much. In other words, @HostBinding('someProperty') someVariable = 'someValue'; means: set the property of someProperty (in the tag element in which I have the selector, obviously) to the someVariable, which I have assigned to someValue. I.E.: if I want set backColor of my host element to red, I should write @HostBinding('style.color') color = 'red' or @HostBinding('style.color') color: string; if I want define the color in other points of my class. And, all is referred to the given selector. Correct?
– Archimede
Nov 12 at 9:39
1
Yes, it's correct, here other example: @HostBinding('style.border-color') borderColor: string; and this.borderColor = 'pink';
– ararb78
Nov 12 at 10:46
Perfectly understood.
– Archimede
Nov 12 at 11:27
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
In Angular, the @HostBinding() function decorator allows you to set the properties of the host element from the directive class.
Let's say you want to change the style properties such as height, width, color, margin, border, etc., or any other internal properties of the host element in the directive class. Here, you'd need to use the @HostBinding() decorator function to access these properties on the host element and assign a value to it in directive class.
The @HostBinding() decorator takes one parameter, the name of the host element property which value we want to assign in the directive.
In Angular, the @HostBinding() function decorator allows you to set the properties of the host element from the directive class.
Let's say you want to change the style properties such as height, width, color, margin, border, etc., or any other internal properties of the host element in the directive class. Here, you'd need to use the @HostBinding() decorator function to access these properties on the host element and assign a value to it in directive class.
The @HostBinding() decorator takes one parameter, the name of the host element property which value we want to assign in the directive.
answered Nov 12 at 9:28
ararb78
1331319
1331319
Thank you very much. In other words, @HostBinding('someProperty') someVariable = 'someValue'; means: set the property of someProperty (in the tag element in which I have the selector, obviously) to the someVariable, which I have assigned to someValue. I.E.: if I want set backColor of my host element to red, I should write @HostBinding('style.color') color = 'red' or @HostBinding('style.color') color: string; if I want define the color in other points of my class. And, all is referred to the given selector. Correct?
– Archimede
Nov 12 at 9:39
1
Yes, it's correct, here other example: @HostBinding('style.border-color') borderColor: string; and this.borderColor = 'pink';
– ararb78
Nov 12 at 10:46
Perfectly understood.
– Archimede
Nov 12 at 11:27
add a comment |
Thank you very much. In other words, @HostBinding('someProperty') someVariable = 'someValue'; means: set the property of someProperty (in the tag element in which I have the selector, obviously) to the someVariable, which I have assigned to someValue. I.E.: if I want set backColor of my host element to red, I should write @HostBinding('style.color') color = 'red' or @HostBinding('style.color') color: string; if I want define the color in other points of my class. And, all is referred to the given selector. Correct?
– Archimede
Nov 12 at 9:39
1
Yes, it's correct, here other example: @HostBinding('style.border-color') borderColor: string; and this.borderColor = 'pink';
– ararb78
Nov 12 at 10:46
Perfectly understood.
– Archimede
Nov 12 at 11:27
Thank you very much. In other words, @HostBinding('someProperty') someVariable = 'someValue'; means: set the property of someProperty (in the tag element in which I have the selector, obviously) to the someVariable, which I have assigned to someValue. I.E.: if I want set backColor of my host element to red, I should write @HostBinding('style.color') color = 'red' or @HostBinding('style.color') color: string; if I want define the color in other points of my class. And, all is referred to the given selector. Correct?
– Archimede
Nov 12 at 9:39
Thank you very much. In other words, @HostBinding('someProperty') someVariable = 'someValue'; means: set the property of someProperty (in the tag element in which I have the selector, obviously) to the someVariable, which I have assigned to someValue. I.E.: if I want set backColor of my host element to red, I should write @HostBinding('style.color') color = 'red' or @HostBinding('style.color') color: string; if I want define the color in other points of my class. And, all is referred to the given selector. Correct?
– Archimede
Nov 12 at 9:39
1
1
Yes, it's correct, here other example: @HostBinding('style.border-color') borderColor: string; and this.borderColor = 'pink';
– ararb78
Nov 12 at 10:46
Yes, it's correct, here other example: @HostBinding('style.border-color') borderColor: string; and this.borderColor = 'pink';
– ararb78
Nov 12 at 10:46
Perfectly understood.
– Archimede
Nov 12 at 11:27
Perfectly understood.
– Archimede
Nov 12 at 11:27
add a comment |
up vote
1
down vote
The :host() CSS pseudo-class function selects the shadow host of the
shadow DOM containing the CSS it is used inside (so you can select a
custom element from inside its shadow DOM) — but only if the selector
given as the function's parameter matches the shadow host.
Ref:https://developer.mozilla.org/en-US/docs/Web/CSS/:host()
Try this it will work
component.css
:host(.alfa)
color: red;
Sorry, is not what I means. Now, I try to explain more clearly in my original question. I am related to the @HostBinding row, nothing else.
– Archimede
Nov 12 at 7:49
you mean the style not applying or what?
– Chellappan
Nov 12 at 8:39
No, the main goal of my question is not focused on the style but on the HostBinding command. I have edited my original question. Thank you very much for your patience!
– Archimede
Nov 12 at 9:05
Angular use shadow DOM to encapsulate the style.That is why your styles are not getting applied
– Chellappan
Nov 12 at 9:15
Thank but the question is not (not) focused to a css / dom problem but to understanding very well the HostBinding instruction.
– Archimede
Nov 12 at 9:31
add a comment |
up vote
1
down vote
The :host() CSS pseudo-class function selects the shadow host of the
shadow DOM containing the CSS it is used inside (so you can select a
custom element from inside its shadow DOM) — but only if the selector
given as the function's parameter matches the shadow host.
Ref:https://developer.mozilla.org/en-US/docs/Web/CSS/:host()
Try this it will work
component.css
:host(.alfa)
color: red;
Sorry, is not what I means. Now, I try to explain more clearly in my original question. I am related to the @HostBinding row, nothing else.
– Archimede
Nov 12 at 7:49
you mean the style not applying or what?
– Chellappan
Nov 12 at 8:39
No, the main goal of my question is not focused on the style but on the HostBinding command. I have edited my original question. Thank you very much for your patience!
– Archimede
Nov 12 at 9:05
Angular use shadow DOM to encapsulate the style.That is why your styles are not getting applied
– Chellappan
Nov 12 at 9:15
Thank but the question is not (not) focused to a css / dom problem but to understanding very well the HostBinding instruction.
– Archimede
Nov 12 at 9:31
add a comment |
up vote
1
down vote
up vote
1
down vote
The :host() CSS pseudo-class function selects the shadow host of the
shadow DOM containing the CSS it is used inside (so you can select a
custom element from inside its shadow DOM) — but only if the selector
given as the function's parameter matches the shadow host.
Ref:https://developer.mozilla.org/en-US/docs/Web/CSS/:host()
Try this it will work
component.css
:host(.alfa)
color: red;
The :host() CSS pseudo-class function selects the shadow host of the
shadow DOM containing the CSS it is used inside (so you can select a
custom element from inside its shadow DOM) — but only if the selector
given as the function's parameter matches the shadow host.
Ref:https://developer.mozilla.org/en-US/docs/Web/CSS/:host()
Try this it will work
component.css
:host(.alfa)
color: red;
answered Nov 12 at 7:39
Chellappan
4,7112419
4,7112419
Sorry, is not what I means. Now, I try to explain more clearly in my original question. I am related to the @HostBinding row, nothing else.
– Archimede
Nov 12 at 7:49
you mean the style not applying or what?
– Chellappan
Nov 12 at 8:39
No, the main goal of my question is not focused on the style but on the HostBinding command. I have edited my original question. Thank you very much for your patience!
– Archimede
Nov 12 at 9:05
Angular use shadow DOM to encapsulate the style.That is why your styles are not getting applied
– Chellappan
Nov 12 at 9:15
Thank but the question is not (not) focused to a css / dom problem but to understanding very well the HostBinding instruction.
– Archimede
Nov 12 at 9:31
add a comment |
Sorry, is not what I means. Now, I try to explain more clearly in my original question. I am related to the @HostBinding row, nothing else.
– Archimede
Nov 12 at 7:49
you mean the style not applying or what?
– Chellappan
Nov 12 at 8:39
No, the main goal of my question is not focused on the style but on the HostBinding command. I have edited my original question. Thank you very much for your patience!
– Archimede
Nov 12 at 9:05
Angular use shadow DOM to encapsulate the style.That is why your styles are not getting applied
– Chellappan
Nov 12 at 9:15
Thank but the question is not (not) focused to a css / dom problem but to understanding very well the HostBinding instruction.
– Archimede
Nov 12 at 9:31
Sorry, is not what I means. Now, I try to explain more clearly in my original question. I am related to the @HostBinding row, nothing else.
– Archimede
Nov 12 at 7:49
Sorry, is not what I means. Now, I try to explain more clearly in my original question. I am related to the @HostBinding row, nothing else.
– Archimede
Nov 12 at 7:49
you mean the style not applying or what?
– Chellappan
Nov 12 at 8:39
you mean the style not applying or what?
– Chellappan
Nov 12 at 8:39
No, the main goal of my question is not focused on the style but on the HostBinding command. I have edited my original question. Thank you very much for your patience!
– Archimede
Nov 12 at 9:05
No, the main goal of my question is not focused on the style but on the HostBinding command. I have edited my original question. Thank you very much for your patience!
– Archimede
Nov 12 at 9:05
Angular use shadow DOM to encapsulate the style.That is why your styles are not getting applied
– Chellappan
Nov 12 at 9:15
Angular use shadow DOM to encapsulate the style.That is why your styles are not getting applied
– Chellappan
Nov 12 at 9:15
Thank but the question is not (not) focused to a css / dom problem but to understanding very well the HostBinding instruction.
– Archimede
Nov 12 at 9:31
Thank but the question is not (not) focused to a css / dom problem but to understanding very well the HostBinding instruction.
– Archimede
Nov 12 at 9:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257512%2fangular-hostbinding-simple-example-and-fundamentals%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Thank but I haven't found in this link any answer to my specific question.
– Archimede
Nov 12 at 7:34