html 代码如下所示:
<div ng-if="name=='John'">
<div class="item item-avatar"> <img ng-src="john.jpg"></div>
</div>
<div ng-if="name=='Margaret'"
<div class="item item-avatar"> <img ng-src="margaret.jpg"></div>
</div>
我也尝试使用 ng-show 而不是 ng-if。两者都不起作用。无论我使用哪个,约翰和玛格丽特都显示在页面上。我也尝试过使用 ng-switch。
我之前在同一个 HTML 文件中初始化的变量“name”:
<a class="item item-avatar item-icon-right" ng-init="name = 'John'" href = "#/Page3"></a>
单击上面的行会转到第 3 页,我需要根据“名称”的值显示约翰或玛格丽特。
是语法错误还是其他什么原因,因为这很有可能。我是 Ionic 和 AngularJS 的新手。
最佳答案
试试这个:
<div ng-show="name=='John'" ng-init="name = 'John'">
<div class="item item-avatar"> John</div>
</div>
<div ng-show="name=='Margaret'"
<div class="item item-avatar"> Margaret</div>
</div>
对我有用。我只是将 ng-if 更改为 ng-show - 当 true 时它将显示 div 内容,否则隐藏它。我还在 div 中使用 ng-init。
关于html - ng-if 和 ng-show 在我的 Ionic 应用程序中都不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30860721/