有条件的ng-show

时间:2017-03-16 08:49:14

标签: angularjs

有没有办法在下面列出条件:

<h3 ng-show="ctrl.loggedIn">
{{ctrl.loggedInUser.FirstName + " " + ctrl.loggedInUser.LastName + " is logged in."}}
</h3>

如果 ctrl.loggedInUser.FirstName 未定义 null ,则无法打印。

点击here访问github上的项目。

4 个答案:

答案 0 :(得分:1)

是的,你可以。使用class Game < ApplicationRecord belongs_to :categorization validate :only_one_most_popular_game scope :most_popular, -> { where(most_popular: true) } protected def only_one_most_popular_game return unless most_popular? matches = Game.most_popular if persisted? matches = matches.where('id != ?', id) end if matches.exists? errors.add(:most_popular, 'Can\'t have another most popular game.') end end end 在DOM中添加条件

ng-if

答案 1 :(得分:0)

h3仅显示ctrl.loggedInUser.FirstName是否具有某个值

<h3 ng-show="ctrl.loggedIn && ( ctrl.loggedInUser.FirstName!=null || (ctrl.loggedInUser.FirstName!='undefined')")>
{{ctrl.loggedInUser.FirstName + " " + ctrl.loggedInUser.LastName + " is logged in."}}
</h3>

你也可以这样做

 <h3 ng-hide="!ctrl.loggedIn && (ctrl.loggedInUser.FirstName==null || (ctrl.loggedInUser.FirstName=='undefined')")>
    {{ctrl.loggedInUser.FirstName + " " + ctrl.loggedInUser.LastName + " is logged in."}}
    </h3>

答案 2 :(得分:0)

您可以使用ng-show

<h3 ng-show="ctrl.loggedInUser.FirstName!=undefined || ctrl.loggedInUser.FirstName!=null">
{{ctrl.loggedInUser.FirstName + " " + ctrl.loggedInUser.LastName + " is logged in."}}
</h3>

答案 3 :(得分:0)

<div ng-switch on="ctrl.loggedInUser.FirstName == null || ctrl.loggedInUser.FirstName == undefined || ctrl.loggedInUser.FirstName == ''">
    <h3 ng-switch-when="true">Please log in.</h3>
    <h3 ng-switch-when="false">{{ctrl.loggedInUser.FirstName + " " + ctrl.loggedInUser.LastName + " is logged in."}}</h3>
</div>
这对我有用。 感谢所有的想法。