2017-03-17 79 views
0

我希望我的按鈕僅在選擇區域中選擇了其中一個選項時才顯示。僅當選擇了選項時顯示按鈕

<div class="row" ng-controller='ctrl1'> 
     <div class ="form_group"> 
      <select class="form-control" ng-options='item as item.hotel_name for item in hotels.data' ng-model='current_Hotel' ng-click="selectHotel()"><option value="" selected disabled>Choose a hotel</option> 
      </select> 
     </div> 

     <div class="col-md-6">  
      <h1>{{current_Hotel.hotel_name}}</h1> 
      <p>{{current_Hotel.hotel_description}}</p> 
      <img class="img img-responsive" ng-src="{{current_Hotel.image_url}}"> 
      <btn class="btn btn-primary">Book a room </btn> 
     </div> 
</div> 

我試過使用ng-show和ng-hide,但它沒有按照我的要求工作。

+0

嘗試對按鈕使用'ng-show'指令。它非常簡單而有效。 –

+0

'ng-show'條件'selectedOptions.length> 0' –

回答

2

使用ng-if指令與多個條件

<div class="col-md-6" ng-if="current_Hotel && current_Hotel != ''"> 
    <h1>{{current_Hotel.hotel_name}}</h1> 
    <p>{{current_Hotel.hotel_description}}</p> 
    <img class="img img-responsive" ng-src="{{current_Hotel.image_url}}"> 
    <btn class="btn btn-primary">Book a room </btn> 
</div> 
+0

這也適用。謝謝 – DragonBorn

+0

沒有問題請務必標記爲答案:D –

1

ngmodel的選擇選項時,選擇將被設置,你需要的,如果選擇的模型檢查或不

<div class="row" ng-controller='ctrl1'> 
     <div class ="form_group"> 
      <select class="form-control" ng-options='item as item.hotel_name for item in hotels.data' ng-model='current_Hotel' ng-click="selectHotel()"><option value="" selected disabled>Choose a hotel</option> 
      </select> 
     </div> 

     <div class="col-md-6">  
      <h1>{{current_Hotel.hotel_name}}</h1> 
      <p>{{current_Hotel.hotel_description}}</p> 
      <img class="img img-responsive" ng-src="{{current_Hotel.image_url}}"> 
      <btn ng-show="current_Hotel" class="btn btn-primary">Book a room </btn> 
     </div> 
</div> 
2

ng-showng-hide應該工作根據您在選擇選項時分配的ng-model的值,假設您在相同的範圍內工作。

你試過ng-show="current_Hotel !== ''"

+1

當我使用ng-show =「current_Hotel.hotel_name!==''」時工作。謝謝 – DragonBorn

+1

@SLASH不客氣!作爲對sachila ranawaka響應的補充(當前選定):我沒有提及 - 如果因爲它從html中刪除了代碼,並且每個摘要循環都執行評估以檢查是否必須再次包含代碼,並且這可能導致性能問題如果過度使用,那麼要小心! – Diego

+0

我會記住這一點。謝謝 – DragonBorn

相關問題