简体   繁体   中英

Angular: how can I make a class member private but accesible for the template?

I have the followign component

component ts:

export class calculator {
    public elements;
    public findAll() {
     // backend stuff for retrieving data and assigning it to elements
    }

    public delete(obj) {
     // backend stuff for deleting obj
    }

    public save(obj) {
     // backend stuff for saving obj
    }
}

the component template:

<div>
  <button (click)="findAll()">Start!</button>
  <div>
     <div *ngFor="let elem for elements">
       <span>{{elem.name}}</span>
       <button (click)="delete(elem)"></button>
     </div>
  </div>
</div>

I would like to only the save method be accesible from outside the class, is there a way to achieve this?

class anotherClass {
   this.calculator.save(formula); // can't access to findAll or delete
}

EDIT: I've try with the protected accesor, but I get the error:

'findAll' is protected and only accessible within class 'calculator' and its subclasses.

No you can't do that.Templates do not exist within component classes, but outside of them. So private members is not accessible for Template

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM