I need my component template to render an email address which may is stored in one property of a class, but should fall back to another propery in case the first is null or empty. So I implemented it this way:
my-component.html:
<p>{{ myService.usuario.emailAddress() }}</p>
my-component.ts:
export class MyComponent implements OnInit {
constructor(private myService: MyService) {
}
}
my-service.ts:
export class MyService {
//...
public user: User;
//...
}
user.ts:
export class User {
//...
mail: string;
userPrincipalName: string;
public emailAddress(): string {
return this.mail || this.userPrincipalName;
}
//..
}
But then the email won't be rendered. However, if I put this inside the template:
<p>{{ myService.usuario.mail || myService.usuario.userPrincipalName }}</p>
Then it will be rendered as intended.
Returnin this.mail ? this.mail : this.userPrincipalName instead of this.mail || this.userPrincipalName did no good either...
EDIT:
In order to better understand what's going on, I changed the template to this:
<p>.mail = {{ azureService.usuario.mail}}</p>
<p>.userPrincipalName = {{ azureService.usuario.userPrincipalName}}</p>
<p>(.mail || .userPrincipalName) = {{ azureService.usuario.mail || azureService.usuario.userPrincipalName}}</p>
<p>.emailAddress() = {{ azureService.usuario.emailAddress()}}</p>
This was the result:
.mail = [email protected]
.userPrincipalName = [email protected]
(.mail || .userPrincipalName) = [email protected]
.emailAddress() =
So...
Is Angular template limited to reading properties but incapable of reading functions' results, as it seems? Or did I something wrong anywhere?
mailanduserPrincipalNamedefined whenemailAddressis called? If Angular callsemailAddressbeforemainanduserPrincipalNameare set, then there is no way for Angular to know that those properties have changed and that it needs to callemailAddressagain.Userobject is instantiated with all its properties, the service changes its flag.authenticatedproperty to true and the component I described in the post is allowed to render. Am I correct about my understanding? I'm used to server side templating (Twig, Django) but I'm new to Angular, I was tweaking a little with this sample.