AngularJS – dependent dropdowns

W jaki sposób tworzyć przy wykorzystaniu angulara dropdowny, które będą od siebie zależne? Stwórzmy prosty kontroler z przykładowymi danymi:

angular.module('shopApp').controller('ProductController', ['$scope', function($scope) {
    $scope.products = {
        'Ice cream': {
            'Chocolate',
            'Vanilla'
        },
        'Pizza' : {
            'Prosciutto',
            'Americana'
        },
        'Cake': {
            'Apple pie',
            'Cheese'
        }
    };
}]);

Następnie przygotujmy widok html:

<script src="~/Scripts/angular/angular.js"></script>
<script src="~/FiltersAngular/app-client.js"></script>
<script src="~/FiltersAngular/productController.js"></script>

<div ng-app="shopApp" ng-controller="ProductController">
        <div class="form-group">          
            <select id="product" ng-model="types" ng-options="product for (product, type) in products">
                <option value=''>Select product</option>
            </select>
        </div>
        <div class="form-group">         
            <select id="type" ng-disabled="!types" ng-model="type" ng-options="type for type in types">
                <option value=''>Select type</option>
            </select>
        </div>
    </div>
</div>

I to wszystko 🙂

Zostaw komentarz

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s