使用angular

时间:2019-06-10 11:58:02

标签: html angular

我正在尝试根据来自服务的响应动态添加/删除html标签。这类似于if-else条件。如果服务有任何响应,则将显示响应,否则将显示默认选项。我不了解如何实现这一目标。我知道我们可以用来显示/隐藏某些东西。这是实现此目标的正确方法吗?

<ng-template #showBlock>
  <p>
    Show this only if "show" is true
  </p>
</ng-template>
<ng-template #notShowBlock>
  <p>
    Show this only if "show" is not true
  </p>
</ng-template>

2 个答案:

答案 0 :(得分:0)

这应该有效...您可以设置输入来切换值,以便预先看到效果

相关的 HTML

select to toggle<input type="checkbox" [(ngModel)]="showNgTemplate" /> {{showNgTemplate}} <br/>
<div class='ngTemplateSection'>
  <ng-template *ngIf='showNgTemplate then showBlock else notShowBlock'>
  </ng-template>
</div>

<ng-template #showBlock> 
  <p>
    Show this only if "show" is true
  </p>
</ng-template> 
<ng-template #notShowBlock>
  <div class='warningStatement'>
    <p>
      Show this only if "show" is not true
    </p>
  </div>
</ng-template>

检查working stackblitz here

答案 1 :(得分:0)

如果您从服务器API得到响应,则只需返回即可

public async Task<int>UpdateValue(modelname obj)
{
if(needtoshow){
return 0;
}
return 1;
}

基于上述条件,如果您在订阅方法之后在TS响应中使用

this.getmethos(modelvalues).subcribe(res=>{
if(res){
  -- wrote your condition to show
}else{
  -- wrote based on response false hide the html tag 
}

})