Is it possible to use a variable in an Angular 2 template that is not on the this context?
For example:
<div>{{ fooBar }}</div>
fooBar needs to be defined in the class used by this component somewhere, like:
export class MyComponent {
public fooBar: boolean = true;
}
But what if you just want to use a value that's not on the class? For example, maybe you want to import a list of string constants and display them selectively in the template:
import { MyService } from './my.service';
import { StringConstants } from '../string.constants';
@Component({
selector: 'my-component',
template: `
<div *ngIf="isPrimary">{{ StringConstants.PRIMARY }}</div>
<div *ngIf="isSecondary">{{ StringConstants.SECONDARY }}</div>
`
})
export class MyComponent {
public isPrimary: boolean;
public isSecondary: boolean;
constructor (private myService: MyService) {
this.isPrimary = myService.getPrimaryContext();
this.isSecondary = myService.getSecondaryContext();
}
};