3

So lets have following markup for PrimeNG table

<p-dataTable selectionMode="multiple" [(selection)]="selectedUsers" dataKey="id" [value]="users"> 

There obviously is a difference between selectionMode="multiple" and [selection]="multiple" as second variand does not make table selectable. What is the difference??

For a moment i thought that property= will set value once while [property]= would bind that property to input value so it would reflect its changes, but if that is the case, in both variants table should behave the same in my case.

3

2 Answers 2

3

The difference is the decorator used under the hood.

For cases like this [property]="" the component ts file implements that property with the @Input decorator.

For cases like this property="" the component ts file implements that property with the @Attribute decorator.

See this example:

export class MyComponent { @Input() public title1: string; public title2: string; constructor(@Attribute('title2') titleAttr) { this.title2 = titleAttr; } } 

So then, you can use it like:

<my-component [title1]="'testing'" title2="testing2"></my-component> 

Another thing to have in mind and be able to decide when to use which one is the type of value you want to pass in. While the input accepts variables coming from your component context, the attribute only accepts values passed in line.

Notice in the example above that for the input I am using single quotes to pass a string in line, but it could be without the quotes and a name of a existing variable.

Sign up to request clarification or add additional context in comments.

Comments

1
selectionMode="multiple" 

Is plain HTML attribute and not related to Angular in any way.

[selection]="multiple" 

creates an Angular binding from the field (or expression) multiple to the property selection

6 Comments

Its not related to angular but it can be related to component like Alejandro explained.
No, it can't. Angular reads attributes if the name matches an input, but you can read it using plain JS as well, so I don't see this related to Angular. The @Attribute() decorator is an instruction to Angular to read an attribute, but that is a different thing than selectionMode="multiple" which is just added to the DOM as-is (as if you wrote it yourself into index.html)
I think that you focuset on right side of expresion while my doubds were regards left side.
And that is my point - in some cases my argument is valid as you pointed out. This is what Alejandro showed me, and what I was referring to. Im new to angular and didnt know the existence of @Atrribute. Thanks for the effort!
Glad to hear you learned something valueable. Lots of things to discover in Angular.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.