Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 681
NDDTConti Avatar asked Nov 09 '18 10:11

NDDTConti


People also ask

How do you access the parent component property in a child component?

In the parent component, declare the property that you want to receive in the child component, say 'ParentId'. While including the child component inside the parent component, bind the 'ParentId' property to the child component using property binding.

Where does the component metadata inherit from?

Metadata and Decorators are not inherited But there is an exception, @Input and @Output decorators are passed down to the child components. The two properties ( @Input() input , @Output() output ) in ComponentA will be visible to ComponentB .

Can we extend component in angular?

Typescript and Angular give you a way to handle this encapsulation. Inherited components! Using class inheritance in TypeScript, you can declare a base component that contains common UI functionality and use it to extend any standard component you'd like.

How does inheritance work in angular?

Inheritance with Angular Components We can create a “parent” component with common properties/functions, followed by a child component that “extends” the parent. The child will inherit the parent's properties and functions but will have its own template, stylesheet and test file.

How to send data to parent from child in Angular 2?

The Child can send data to Parent by raising an event, Parent can interact with the child via local variable or Parent can call @ViewChild on the child. We will look at all those options in this article. Applies to: Angular 2 to the latest edition of i.e. Angular 8. Angular 9, Angular 10, Angular 11, Angular 12

Is the content still applicable for all angular 2 + versions?

The content is likely still applicable for all Angular 2 + versions. Angular components are a fantastic way to break up our web apps into small easy to understand pieces of UI code. Sometimes in specific apps, we have drastic layout differences for the same data being displayed.

How do I implement a button on a page in angular?

A simple way to implement this is to create a button, call a method in the code which then uses the Angular router to navigate to the page. And what if you didn’t want to repeat the same code in each component? Typescript and Angular give you a way to handle this encapsulation. Inherited components!

When to use component inheritance?

In this particular use case Component Inheritance is rather helpful when our templates have drastic differences in markup but display the same data. First, we need to define our base class that contains all of the logic we want to share. Our base employee component has one input and one output.


1 Answers

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; }
like image 183
AlesD Avatar answered Sep 20 '22 14:09

AlesD