1

Here's the code in my view...

<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>

Here's the relevant controller code...

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

The purpose of this code is pretty simple. If the restaurant.type is "Individual" there should be no restaurant_group set. Otherwise, the restaurant_group should be visible and setable. The problem is that the restaurant_group doesn't seem to stay set.

For example... My starting conditions are restaurant.type = "Corporate" and the restaurant.restaurant_group = { "id": 1, "name": "Something" }. both select boxes show this correctly. When I select "Individual" for the restaurant type, the restaurant group select box disappears and the console logs...

{}

This is what I expect. However, when I change the restaurant type back to "Corporate" the restaurant group select box reappears with the "Something" option already selected. The console log immediately displays...

{"id": 1, "name": "Something"}

This is not what I would expect. I would expect the restaurant group to remain an empty object until I set it again with the restaurant group select box. I'm obviously missing something that is going on here.

1
  • Can you create a plnkr for this ? Commented Sep 22, 2015 at 11:11

4 Answers 4

2

I expect this is happening because restaurant.restaurant_group is bound to the restaurant-group-select inside your div with ng-if="restaurant.type == 'Franchise' || restaurant.type == 'Corporate'".

So when you select a new restaurant type, the ng-if is resolving to true and restaurant.restaurant_group is picking up the default value from restaurant-group-select (usually the top one in the list).

Sign up to request clarification or add additional context in comments.

1 Comment

I wonder if the unwanted behavior could be avoided by using ng-show instead of ng-if.
1

Can you share Plnkr for this?

Its working for me. Please check.

Plunkrhttp://plnkr.co/edit/GUAJHc6n324zNdTTkbqL?p=preview

Comments

1

I think the problem is due to ng-if..Try using ng-show in place ng-if

Comments

0

Thanks guys. The problem ended up being something besides my Angular code. We're using REST services on the backend. The Angular code is changing the restaurant_group to an empty object. Then it is updating the data and refreshing. I expected the backend to remove the restaurant_group if it was set to null or an empty object. Instead it is simply ignoring that property. So, on refresh the backend is repopulating the scope data.

Sorry if I wasted your time looking at this. Turns out that the Angular side of things is working just like it should.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.