I have a global variable to store the list of countries like this:
export var COUNTRY_CODES = ["AD", "AE", "AF" /* and more */];
In one of my component, I'd imported the variable using normal import statement
import { COUNTRY_CODES } from "../constants";
I am able to access this global variable freely in my component code, but failed to achieve something like this on the HTML template:
<option *ngFor="let countryCode of COUNTRY_CODES" [value]="countryCode">{{countryCode | countryName}}</option>
I could just pass along the global variable to component by defining a local variable and assign the global variable to it during initialization.
ngOnInit() {
this.countryCodes = COUNTRY_CODES;
}
And change the ngFor
to loop on this local variable to make it works.
My question: Is this the right way to do? I'm not totally comfortable with defining bridging variables every time I want to use global variables in my template.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…