angular6: I want my inherited component to have some propertys of the parent









up vote
7
down vote

favorite
2












I have a table where every table-cell is a different instance of a component. Depending on the data (date, text, links) I'm using different components.



As these components have a lot in common I introduced a TableCellMasterComponent which is extended by all other type of table-cells.



All my components have the same host property:



@Component(
selector: 'td[app-text-col]',
templateUrl: './text-col.component.html',
styleUrls: ['./text-col.component.css'],
host:
"[hidden]": "col.deactivated"
,
changeDetection: ChangeDetectionStrategy.OnPush
)
export class TextColComponent extends TableCellMasterComponent



Is it possible to somehow move this to the TableCellMasterComponent?



Also I would love to give all of them a contextmenu. But as I see it, it isn't possible to add HTML in the Master. Is that true?



Can I move the changeDetection to the Master?










share|improve this question





















  • Check out this blog post about Encapsulation. Maybe you'll find what you're looking for.
    – ams
    Nov 19 at 23:28














up vote
7
down vote

favorite
2












I have a table where every table-cell is a different instance of a component. Depending on the data (date, text, links) I'm using different components.



As these components have a lot in common I introduced a TableCellMasterComponent which is extended by all other type of table-cells.



All my components have the same host property:



@Component(
selector: 'td[app-text-col]',
templateUrl: './text-col.component.html',
styleUrls: ['./text-col.component.css'],
host:
"[hidden]": "col.deactivated"
,
changeDetection: ChangeDetectionStrategy.OnPush
)
export class TextColComponent extends TableCellMasterComponent



Is it possible to somehow move this to the TableCellMasterComponent?



Also I would love to give all of them a contextmenu. But as I see it, it isn't possible to add HTML in the Master. Is that true?



Can I move the changeDetection to the Master?










share|improve this question





















  • Check out this blog post about Encapsulation. Maybe you'll find what you're looking for.
    – ams
    Nov 19 at 23:28












up vote
7
down vote

favorite
2









up vote
7
down vote

favorite
2






2





I have a table where every table-cell is a different instance of a component. Depending on the data (date, text, links) I'm using different components.



As these components have a lot in common I introduced a TableCellMasterComponent which is extended by all other type of table-cells.



All my components have the same host property:



@Component(
selector: 'td[app-text-col]',
templateUrl: './text-col.component.html',
styleUrls: ['./text-col.component.css'],
host:
"[hidden]": "col.deactivated"
,
changeDetection: ChangeDetectionStrategy.OnPush
)
export class TextColComponent extends TableCellMasterComponent



Is it possible to somehow move this to the TableCellMasterComponent?



Also I would love to give all of them a contextmenu. But as I see it, it isn't possible to add HTML in the Master. Is that true?



Can I move the changeDetection to the Master?










share|improve this question













I have a table where every table-cell is a different instance of a component. Depending on the data (date, text, links) I'm using different components.



As these components have a lot in common I introduced a TableCellMasterComponent which is extended by all other type of table-cells.



All my components have the same host property:



@Component(
selector: 'td[app-text-col]',
templateUrl: './text-col.component.html',
styleUrls: ['./text-col.component.css'],
host:
"[hidden]": "col.deactivated"
,
changeDetection: ChangeDetectionStrategy.OnPush
)
export class TextColComponent extends TableCellMasterComponent



Is it possible to somehow move this to the TableCellMasterComponent?



Also I would love to give all of them a contextmenu. But as I see it, it isn't possible to add HTML in the Master. Is that true?



Can I move the changeDetection to the Master?







angular inheritance components angular6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 10:09









NDDTConti

3810




3810











  • Check out this blog post about Encapsulation. Maybe you'll find what you're looking for.
    – ams
    Nov 19 at 23:28
















  • Check out this blog post about Encapsulation. Maybe you'll find what you're looking for.
    – ams
    Nov 19 at 23:28















Check out this blog post about Encapsulation. Maybe you'll find what you're looking for.
– ams
Nov 19 at 23:28




Check out this blog post about Encapsulation. Maybe you'll find what you're looking for.
– ams
Nov 19 at 23:28












2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted
+50










The @Component decorator metadata is not inherited so you cannot move some things to the base class. @Input and @Output properties get inherited.



There is a solution for the host property you can use a @HostBinding instead and this will get inherited. For example your binding you can do like this:



@HostBinding('hidden') get hidden(): boolean return col.deactivated; 





share|improve this answer




















  • I tried this, the problem with this is that the property col.deactivated changes over time and with the old solution the columns would react to it. With this solution they don't. Sadly I can't bind to [hidden]
    – NDDTConti
    Nov 15 at 9:22










  • Can you give more information about where the col.deactivated comes from? I have made a simple sample on StackBlitz and it works ok. I'm binding to a property of the same component.
    – AlesD
    Nov 15 at 19:58










  • Yeah but in your example it is only checked on startup. There is no change later. On my example the col-object is handed in as an @Input
    – NDDTConti
    Nov 20 at 7:53










  • It changes if you press the Toggle button. Then you can see live that it updates the binding and the content is hidden or shown.
    – AlesD
    Nov 20 at 22:51










  • you are totally right and it also works, must have been tired when I tested this, thanks so much
    – NDDTConti
    Nov 21 at 8:47

















up vote
2
down vote













I also created a Stackblitz which solves them the only way I know how. Credit to AlesD for solving your first problem, all you needed to do to solve the issue is add @Input() onto visibility which you control in the parent element.



Your issue with a context menu is less straightforward. One option that I quickly tried to show in my stackblitz using ngx-contextmenu is done using nested components. There's a nice tutorial here



As you discovered, you have to choose between using extends, or using a nested component. In my example, I use both, but it may make the most sense to just use one or the other depending:



  • If your context menus are different and redundancy is not that big an issue, use extends, and build the context menu manually on each component

  • if the context menus are all going to be the same, I would drop the extends entirely and use a nested component. You can access the nested component using @ViewChild to gain access to the attributes you need.

  • Using both (like in my example) was used mostly for demonstrating as I don't understand the whole context of your application, but if you could abstract your cells to use a common interface or class you can pass in to the contextMenu, limiting the number of inputs, it may make sense for you.





share|improve this answer




















  • I would love to also award you a bounty, sadly I can only do that once, thanks so much for your help
    – NDDTConti
    Nov 21 at 8:47










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',
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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53223686%2fangular6-i-want-my-inherited-component-to-have-some-propertys-of-the-parent%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
4
down vote



accepted
+50










The @Component decorator metadata is not inherited so you cannot move some things to the base class. @Input and @Output properties get inherited.



There is a solution for the host property you can use a @HostBinding instead and this will get inherited. For example your binding you can do like this:



@HostBinding('hidden') get hidden(): boolean return col.deactivated; 





share|improve this answer




















  • I tried this, the problem with this is that the property col.deactivated changes over time and with the old solution the columns would react to it. With this solution they don't. Sadly I can't bind to [hidden]
    – NDDTConti
    Nov 15 at 9:22










  • Can you give more information about where the col.deactivated comes from? I have made a simple sample on StackBlitz and it works ok. I'm binding to a property of the same component.
    – AlesD
    Nov 15 at 19:58










  • Yeah but in your example it is only checked on startup. There is no change later. On my example the col-object is handed in as an @Input
    – NDDTConti
    Nov 20 at 7:53










  • It changes if you press the Toggle button. Then you can see live that it updates the binding and the content is hidden or shown.
    – AlesD
    Nov 20 at 22:51










  • you are totally right and it also works, must have been tired when I tested this, thanks so much
    – NDDTConti
    Nov 21 at 8:47














up vote
4
down vote



accepted
+50










The @Component decorator metadata is not inherited so you cannot move some things to the base class. @Input and @Output properties get inherited.



There is a solution for the host property you can use a @HostBinding instead and this will get inherited. For example your binding you can do like this:



@HostBinding('hidden') get hidden(): boolean return col.deactivated; 





share|improve this answer




















  • I tried this, the problem with this is that the property col.deactivated changes over time and with the old solution the columns would react to it. With this solution they don't. Sadly I can't bind to [hidden]
    – NDDTConti
    Nov 15 at 9:22










  • Can you give more information about where the col.deactivated comes from? I have made a simple sample on StackBlitz and it works ok. I'm binding to a property of the same component.
    – AlesD
    Nov 15 at 19:58










  • Yeah but in your example it is only checked on startup. There is no change later. On my example the col-object is handed in as an @Input
    – NDDTConti
    Nov 20 at 7:53










  • It changes if you press the Toggle button. Then you can see live that it updates the binding and the content is hidden or shown.
    – AlesD
    Nov 20 at 22:51










  • you are totally right and it also works, must have been tired when I tested this, thanks so much
    – NDDTConti
    Nov 21 at 8:47












up vote
4
down vote



accepted
+50







up vote
4
down vote



accepted
+50




+50




The @Component decorator metadata is not inherited so you cannot move some things to the base class. @Input and @Output properties get inherited.



There is a solution for the host property you can use a @HostBinding instead and this will get inherited. For example your binding you can do like this:



@HostBinding('hidden') get hidden(): boolean return col.deactivated; 





share|improve this answer












The @Component decorator metadata is not inherited so you cannot move some things to the base class. @Input and @Output properties get inherited.



There is a solution for the host property you can use a @HostBinding instead and this will get inherited. For example your binding you can do like this:



@HostBinding('hidden') get hidden(): boolean return col.deactivated; 






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 at 22:15









AlesD

2,09829




2,09829











  • I tried this, the problem with this is that the property col.deactivated changes over time and with the old solution the columns would react to it. With this solution they don't. Sadly I can't bind to [hidden]
    – NDDTConti
    Nov 15 at 9:22










  • Can you give more information about where the col.deactivated comes from? I have made a simple sample on StackBlitz and it works ok. I'm binding to a property of the same component.
    – AlesD
    Nov 15 at 19:58










  • Yeah but in your example it is only checked on startup. There is no change later. On my example the col-object is handed in as an @Input
    – NDDTConti
    Nov 20 at 7:53










  • It changes if you press the Toggle button. Then you can see live that it updates the binding and the content is hidden or shown.
    – AlesD
    Nov 20 at 22:51










  • you are totally right and it also works, must have been tired when I tested this, thanks so much
    – NDDTConti
    Nov 21 at 8:47
















  • I tried this, the problem with this is that the property col.deactivated changes over time and with the old solution the columns would react to it. With this solution they don't. Sadly I can't bind to [hidden]
    – NDDTConti
    Nov 15 at 9:22










  • Can you give more information about where the col.deactivated comes from? I have made a simple sample on StackBlitz and it works ok. I'm binding to a property of the same component.
    – AlesD
    Nov 15 at 19:58










  • Yeah but in your example it is only checked on startup. There is no change later. On my example the col-object is handed in as an @Input
    – NDDTConti
    Nov 20 at 7:53










  • It changes if you press the Toggle button. Then you can see live that it updates the binding and the content is hidden or shown.
    – AlesD
    Nov 20 at 22:51










  • you are totally right and it also works, must have been tired when I tested this, thanks so much
    – NDDTConti
    Nov 21 at 8:47















I tried this, the problem with this is that the property col.deactivated changes over time and with the old solution the columns would react to it. With this solution they don't. Sadly I can't bind to [hidden]
– NDDTConti
Nov 15 at 9:22




I tried this, the problem with this is that the property col.deactivated changes over time and with the old solution the columns would react to it. With this solution they don't. Sadly I can't bind to [hidden]
– NDDTConti
Nov 15 at 9:22












Can you give more information about where the col.deactivated comes from? I have made a simple sample on StackBlitz and it works ok. I'm binding to a property of the same component.
– AlesD
Nov 15 at 19:58




Can you give more information about where the col.deactivated comes from? I have made a simple sample on StackBlitz and it works ok. I'm binding to a property of the same component.
– AlesD
Nov 15 at 19:58












Yeah but in your example it is only checked on startup. There is no change later. On my example the col-object is handed in as an @Input
– NDDTConti
Nov 20 at 7:53




Yeah but in your example it is only checked on startup. There is no change later. On my example the col-object is handed in as an @Input
– NDDTConti
Nov 20 at 7:53












It changes if you press the Toggle button. Then you can see live that it updates the binding and the content is hidden or shown.
– AlesD
Nov 20 at 22:51




It changes if you press the Toggle button. Then you can see live that it updates the binding and the content is hidden or shown.
– AlesD
Nov 20 at 22:51












you are totally right and it also works, must have been tired when I tested this, thanks so much
– NDDTConti
Nov 21 at 8:47




you are totally right and it also works, must have been tired when I tested this, thanks so much
– NDDTConti
Nov 21 at 8:47












up vote
2
down vote













I also created a Stackblitz which solves them the only way I know how. Credit to AlesD for solving your first problem, all you needed to do to solve the issue is add @Input() onto visibility which you control in the parent element.



Your issue with a context menu is less straightforward. One option that I quickly tried to show in my stackblitz using ngx-contextmenu is done using nested components. There's a nice tutorial here



As you discovered, you have to choose between using extends, or using a nested component. In my example, I use both, but it may make the most sense to just use one or the other depending:



  • If your context menus are different and redundancy is not that big an issue, use extends, and build the context menu manually on each component

  • if the context menus are all going to be the same, I would drop the extends entirely and use a nested component. You can access the nested component using @ViewChild to gain access to the attributes you need.

  • Using both (like in my example) was used mostly for demonstrating as I don't understand the whole context of your application, but if you could abstract your cells to use a common interface or class you can pass in to the contextMenu, limiting the number of inputs, it may make sense for you.





share|improve this answer




















  • I would love to also award you a bounty, sadly I can only do that once, thanks so much for your help
    – NDDTConti
    Nov 21 at 8:47














up vote
2
down vote













I also created a Stackblitz which solves them the only way I know how. Credit to AlesD for solving your first problem, all you needed to do to solve the issue is add @Input() onto visibility which you control in the parent element.



Your issue with a context menu is less straightforward. One option that I quickly tried to show in my stackblitz using ngx-contextmenu is done using nested components. There's a nice tutorial here



As you discovered, you have to choose between using extends, or using a nested component. In my example, I use both, but it may make the most sense to just use one or the other depending:



  • If your context menus are different and redundancy is not that big an issue, use extends, and build the context menu manually on each component

  • if the context menus are all going to be the same, I would drop the extends entirely and use a nested component. You can access the nested component using @ViewChild to gain access to the attributes you need.

  • Using both (like in my example) was used mostly for demonstrating as I don't understand the whole context of your application, but if you could abstract your cells to use a common interface or class you can pass in to the contextMenu, limiting the number of inputs, it may make sense for you.





share|improve this answer




















  • I would love to also award you a bounty, sadly I can only do that once, thanks so much for your help
    – NDDTConti
    Nov 21 at 8:47












up vote
2
down vote










up vote
2
down vote









I also created a Stackblitz which solves them the only way I know how. Credit to AlesD for solving your first problem, all you needed to do to solve the issue is add @Input() onto visibility which you control in the parent element.



Your issue with a context menu is less straightforward. One option that I quickly tried to show in my stackblitz using ngx-contextmenu is done using nested components. There's a nice tutorial here



As you discovered, you have to choose between using extends, or using a nested component. In my example, I use both, but it may make the most sense to just use one or the other depending:



  • If your context menus are different and redundancy is not that big an issue, use extends, and build the context menu manually on each component

  • if the context menus are all going to be the same, I would drop the extends entirely and use a nested component. You can access the nested component using @ViewChild to gain access to the attributes you need.

  • Using both (like in my example) was used mostly for demonstrating as I don't understand the whole context of your application, but if you could abstract your cells to use a common interface or class you can pass in to the contextMenu, limiting the number of inputs, it may make sense for you.





share|improve this answer












I also created a Stackblitz which solves them the only way I know how. Credit to AlesD for solving your first problem, all you needed to do to solve the issue is add @Input() onto visibility which you control in the parent element.



Your issue with a context menu is less straightforward. One option that I quickly tried to show in my stackblitz using ngx-contextmenu is done using nested components. There's a nice tutorial here



As you discovered, you have to choose between using extends, or using a nested component. In my example, I use both, but it may make the most sense to just use one or the other depending:



  • If your context menus are different and redundancy is not that big an issue, use extends, and build the context menu manually on each component

  • if the context menus are all going to be the same, I would drop the extends entirely and use a nested component. You can access the nested component using @ViewChild to gain access to the attributes you need.

  • Using both (like in my example) was used mostly for demonstrating as I don't understand the whole context of your application, but if you could abstract your cells to use a common interface or class you can pass in to the contextMenu, limiting the number of inputs, it may make sense for you.






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 20:04









Murphy4

505513




505513











  • I would love to also award you a bounty, sadly I can only do that once, thanks so much for your help
    – NDDTConti
    Nov 21 at 8:47
















  • I would love to also award you a bounty, sadly I can only do that once, thanks so much for your help
    – NDDTConti
    Nov 21 at 8:47















I would love to also award you a bounty, sadly I can only do that once, thanks so much for your help
– NDDTConti
Nov 21 at 8:47




I would love to also award you a bounty, sadly I can only do that once, thanks so much for your help
– NDDTConti
Nov 21 at 8:47

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53223686%2fangular6-i-want-my-inherited-component-to-have-some-propertys-of-the-parent%23new-answer', 'question_page');

);

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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)