Ole Spaarmann
Ole Spaarmann

Reputation: 16761

Angular 2: What is the exact difference between private and public attributes in components?

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";
  }

}

Upvotes: 13

Views: 21986

Answers (4)

ian_sawyer
ian_sawyer

Reputation: 221

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

Upvotes: 18

Atal Kishore
Atal Kishore

Reputation: 4748

Answer is for Angular 2 Using Typescript

A typescript file is always compiled to javascript and since there is no access specifier in javascript so

  1. A private variable declared in typescript will be compiled as local variable whereas

  2. A public variable will get compiled as global variable(i.e outside the function body in javascript).

If you dont mention access specifier by default it is private so private_text and other_text has no difference

Upvotes: 4

daddywoodland
daddywoodland

Reputation: 1512

private and public are typescript concepts, not Angular2.

Writing in typescript is encouraged because it is a mechanism for enforcing some rules on the JS you're writing, which should be highlighted when you transpile to ES5.

Using private variables in classes is leveraged by the dependency injection in Angular2 as well, to save you having explicitly assign them in the constructor too.

For example -

export class LoggingService {

    constructor(private _appConfig: AppConfig) { }

    someMethod() {

      // you can use this._appConfig without having to explicitly define it

    }
}

Upvotes: 4

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657781

private and public is something that is only utilized by tools that statically analyze TypeScript code.

At runtime private and public are meaningless because JS doesn't know about them.

TypeScript tools also are not checking the template string for binding expressions that might violate TypeScript rules. This is the reason that it doesn't matter for the template whether the property is private or public.

As far as I know it is planned to improve template analyzing functionality to recognize all kind of mistakes. But that's only the future.

Upvotes: 13

Related Questions