2015-09-22 84 views
1

這裏是我認爲的代碼...角度變化

<label for="restaurant-type-select" class="control-label"> 
    Type 
</label> 
<select class="form-control" id="restaurant-type-select" ng-model="restaurant.type" ng-change="restaurantTypeChanged()"> 
    <option value="Franchise">Franchise</option> 
    <option value="Corporate">Corporate</option> 
    <option value="Individual">Individual</option> 
</select> 
<div ng-if="restaurant.type == 'Franchise' || restaurant.type == 'Corporate'"> 
    <label for="restaurant-group-select" class="control-label"> 
     Restaurant Group 
    </label> 
    <select class="form-control" id="restaurant-group-select" ng-model="restaurant.restaurant_group" ng-options="restaurant_group.name for restaurant_group in restaurant_groups.items track by restaurant_group.id"> 
    </select> 
</div> 

下面是相關的控制器代碼...

$scope.restaurantTypeChanged = function() { 
    if ($scope.restaurant.type == "Individual") { 
     $scope.restaurant.restaurant_group = {}; 
    } 
    console.log($scope.restaurant.restaurant_group); 
}; 

這段代碼的目的非常簡單。如果restaurant.type爲「Individual」,則不應設置restaurant_group。否則,restaurant_group應該是可見和可封閉的。問題是restaurant_group似乎沒有保持設置。

例如...我的啓動條件爲restaurant.type = 「公司」 和restaurant.restaurant_group = { 「ID」:1, 「名」: 「東西」}。兩個選擇框都正確顯示。當我選擇「個人」爲餐廳類型,餐飲集團選擇框消失,在控制檯日誌...

{}

這是我所期望的。但是,當我將餐廳類型更改回「公司」時,餐館組選擇框會重新出現,並且已選擇「某些」選項。控制檯登錄立即顯示...

{ 「ID」:1, 「名」: 「東西」}

這不是我所期望的。我希望餐廳組保持空的對象,直到我再次用餐廳組選擇框設置它。我明顯錯過了這裏正在發生的事情。

+0

你能爲此創建一個plnkr嗎? – Fahad

回答

2

我預計會發生這種情況,因爲restaurant.restaurant_group必須與您的div中的restaurant-group-select綁定,並且ng-if="restaurant.type == 'Franchise' || restaurant.type == 'Corporate'"

因此,當您選擇新的餐廳類型時,ng-if解析爲true,restaurant.restaurant_group正在從restaurant-group-select(通常是列表中最高的一個)中選取默認值。

+0

我不知道是否可以通過使用ng-show而不是ng-if來避免不需要的行爲。 – saeraphin

1

我覺得這個問題是由於採用NG-秀到位NG-如果

0

謝謝你們到NG-if..Try。問題最終成爲除了我的Angular代碼之外的東西。我們在後端使用REST服務。 Angular代碼正在將restaurant_group改爲空對象。然後它正在更新數據並刷新。我期望後端刪除restaurant_group,如果它被設置爲null或空對象。相反,它只是無視該財產。因此,在刷新後端時重新填充範圍數據。

對不起,如果我浪費你的時間看着這個。事實證明,事情的角度是正常工作。