html - ng-if 和 ng-show 在我的 Ionic 应用程序中都不起作用

标签 html angularjs ionic-framework

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/

相关文章:

html - Angular指令novalidate在表单html标签中的用法是什么

javascript - Angular JS ng-if 在链接上

javascript - 我可以手动触发 ngMessages 验证吗?

angular - 覆盖 ionic 4中的工具栏后退按钮操作

javascript - 按钮 ng-click 更改 URL 但不呈现新 View ?

c# - 将每个用户输入处理为纯文本

javascript - 折叠标签 - 跳转到打开的标签

javascript - 用 div 替换显示隐藏按钮

javascript - AngularJS - 如果我不提交,如何保持原始值

javascript - ng-click 不会执行