Angular 5 [ngClass]更改语言设置

时间:2018-01-22 21:04:48

标签: angular angular-ng-class

以下静态html <ul>下拉列表(包含下面的样式)完美运行。 现在,我尝试使用<li>*ngFor*将其转换为Angular 5动态ngClass

&#13;
&#13;
<!-- LANG SELECT - TEST -->
<ul class="nav-item header-dropdown-list hidden-xs">
	<li class="open">
		<a href="#" class="nav-link dropdown-toggle my-1" data-toggle="dropdown" aria-expanded="true"> 
			<img src="./assets/images/blank.gif" class="flag flag-en" alt="United States"> <span> US</span> 
			<!-- <img src="./assets/images/blank.gif" [ngClass]="getSelFlagClass(lang)" alt="United States" #selectedLanguage> <span> US</span>  -->
			
		</a>
		<ul class="dropdown-menu pull-right">
			<!-- <li *ngFor="let lang of translate.getLangs()" 
				[class.active]="clicked === lang"
				[value]="lang" 
				(click)="lang === translate.currentLang" >

				<a href="#"><img src="./assets/images/blank.gif" [ngClass]="getFlagClass(lang)" alt="United States"> {{lang}}</a>
			</li> -->
			<li class="">
				<a href="javascript:void(0);"><img src="./assets/images/blank.gif" class="flag flag-en" alt="United States"> English (US)</a>
			</li>
			<li>
				<a href="javascript:void(0);"><img src="./assets/images/blank.gif" class="flag flag-fr" alt="France"> Français</a>
			</li>
			<li>
				<a href="javascript:void(0);"><img src="./assets/images/blank.gif" class="flag flag-sp" alt="Spanish"> Español</a>
			</li>
			<li>
				<a href="javascript:void(0);"><img src="./assets/images/blank.gif" class="flag flag-de" alt="German"> Deutsch</a>
			</li>
			<li>
				<a href="javascript:void(0);"><img src="./assets/images/blank.gif" class="flag flag-jp" alt="Japan"> 日本語</a>
			</li>
			<li>
				<a href="javascript:void(0);"><img src="./assets/images/blank.gif" class="flag flag-cn" alt="China"> 中文</a>
			</li>	
			<li>
				<a href="javascript:void(0);"><img src="./assets/images/blank.gif" class="flag flag-it" alt="Italy"> Italiano</a>
			</li>	
			<li>
				<a href="javascript:void(0);"><img src="./assets/images/blank.gif" class="flag flag-pt" alt="Portugal"> Portugal</a>
			</li>
			<li>
				<a href="javascript:void(0);"><img src="./assets/images/blank.gif" class="flag flag-ru" alt="Russia"> Русский язык</a>
			</li>
			<li>
				<a href="javascript:void(0);"><img src="./assets/images/blank.gif" class="flag flag-kr" alt="Korea"> 한국어</a>
			</li>
		</ul>
	</li>
</ul>
&#13;
&#13;
&#13;

&#13;
&#13;
<style>
	
	.flag {
		width: 16px;
		height: 11px;
		background: url(./assets/images/flags.png) no-repeat;
		margin-top: -.18em;
		display: inline-block
	}
	
	.flag.flag-en {		
		background-position: -144px -154px
	}
	.flag.flag-sp {	
		background-position: -16px -44px
	}
    /* additional flag styles omitted */

	.flag {
		text-transform: capitalize;
		display: inline-block;
		max-width: 150px;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		vertical-align: middle;
		margin-top: -3px
	}
	
	@media (min-width: 768px) and (max-width:979px) {
		.flag+span {
			max-width:30px!important
		}
	}
	

	.header-dropdown-list {
		list-style: none;
		float: right;
		position: relative;
		margin: 0;
		padding-left: 0;
		padding-right: 0
	}
	</style>
&#13;
&#13;
&#13;

Angular版本看起来像这样,但是我无法在加载时显示顶部<a href>标志(当我重新选择一种语言时也是如此)。

如果您查看下面的屏幕截图,您会看到我的初始标记未显示在<ul>顶部US。 我认为我应该使用@ViewChild()处理这个问题,但我不确定 - 或者可能使用@HostBinding()

&#13;
&#13;
<ul class="nav-item header-dropdown-list hidden-xs">
	<li class="open">
		<a href="#" class="nav-link dropdown-toggle my-1" data-toggle="dropdown" aria-expanded="true"> 
			
			<img src="./assets/images/blank.gif" [ngClass]="getSelFlagClass(lang)" alt="United States" #selectedLanguage> <span> US</span> 
			
		</a>
		<ul class="dropdown-menu pull-right">
			<li *ngFor="let lang of translate.getLangs()" 
				[class.active]="clicked === lang"
				[value]="lang" 
				(click)="lang === translate.currentLang" >

				<a href="#"><img src="./assets/images/blank.gif" [ngClass]="getFlagClass(lang)" alt="United States"> {{lang}}</a>
			</li>
		</ul>
	</li>
</ul>
&#13;
&#13;
&#13;

&#13;
&#13;
  getFlagClass(sel){
    return "flag flag-" + sel;
  }
  getSelFlagClass(sel){
    // i.e.  class="flag flag-us"
    return "flag flag-" + sel;
  }
&#13;
&#13;
&#13;

enter image description here

0 个答案:

没有答案