Could someone explain the difference between private and not private attributes in components in Angular 2? Like with private_text
and other_text
in this example:
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'my-component',
template: '<div>{{private_text}}</div><div>{{other_text}}</div>'
})
export class MyComponent {
private private_text:string;
other_text:string;
constructor() {
this.private_text = "hello";
this.other_text = "hello";
}
}
Public:
Typescript members are public by default.
So if I have a class myClass
with the method public myMethod() {}
or just myMethod(){}
then import my class into another file. I now define in my constructor of another class constructor(my_class: myClass) {}
. This now lets me call this.my_class.myMethod()
wherever I want in my other class. If it was private. This would not work.
Private:
"When a member is marked private, it cannot be accessed from outside of its containing class"
Really confused why no one has referenced this yet. I think the following link will really help.
https://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With