2017-03-16 77 views
0

有一個NG-如果鏈接顯示一個按鈕:Angular 1.x Ng-If語句適用於&&但不是||,爲什麼?

<button ng-if="myOwnProfile == false || AreWeFriends == false" class="btn btn-default" ng-click="connect($id)">connect</button> 

我想讓它顯示如果任是假的;但是,使用此代碼myOwnProfile永遠不會被評估,並且不起作用。如果用戶正在查看他們自己的配置文件,並且如果用戶與某人不是「朋友」,則該按鈕會消失。

聲明做工精細自己,但是當我使用||,它拒絕工作:( 當我使用「& &」的聲明,他們不正確的評價,但是,爲什麼會使用時,這種邏輯工作?「」我想要做一個非此即彼/或

+0

使用'!'(否定)運算符而不是'== false'! – Bergi

+0

如果您需要將類型設爲布爾值,請使用'==='(身份)運算符 – cnorthfield

回答

1

條件不表明:ownprofile並好友

所以顯示的條件:不ownprofile或朋友

angular.module('app', []).controller('myCtrl', function($scope) { 
 
    $scope.myOwnProfile = true; 
 
    $scope.AreWeFriends = true; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="myCtrl"><button ng-if="myOwnProfile == false || AreWeFriends == true" class="btn btn-default" ng-click="connect($id)">connect</button> 
 
    <button ng-click="myOwnProfile = !myOwnProfile">My Own Profile</button> 
 
    <button ng-click="AreWeFriends = !AreWeFriends">AreWeFriends</button> 
 

 
    <div> 
 
    myOwnProfile: {{ myOwnProfile }} <br> AreWeFriends: {{ AreWeFriends }} 
 
    </div> 
 
</div>

+0

謝謝,該按鈕並未實際運行。如果加載的配置文件是a)用戶自己的配置文件或b)他們已經連接到的配置文件,則將其隱藏。 所以 - 我在控制器中檢查了$ scope.myOwnProfile爲true或AreWeFriends爲true,如果它們在if語句中籤出。 所以 - 這讓我感到困惑,爲什麼它出現在==。 :) – nashvilleCoder

+0

@nashvilleCoder代碼片段中的按鈕僅用於測試。我認爲這不是關於||的聲明,您必須查看您的情況,以瞭解按鈕何時顯示。 – Pengyy

+0

謝謝。我對邏輯有點困惑,我意識到我從來沒有確定我不會成爲我自己的朋友(在我自己的個人資料中),所以當我查看個人資料時總是顯示...哎呀。這就是&&爲什麼... :) 謝謝你的幫助。 – nashvilleCoder

相關問題