How to make a select using Angularjs?

Asked

Viewed 774 times

1

I’m trying to make a select field in mine view and I’m using the framework Angular.js, but I’m not used to working with it and I’m not getting the job done on controller. Below is the example of the code I tried to do that didn’t work.

$scope.classificacaoProduto = function(idClassificacaoProduto) {
            if(idClassificacaoProduto == undefined)
                return 'Selecione';
            $http({method: 'GET', params: $scope.filtro ,url: "produtos/"+idClassificacaoProduto}).success(function(data){
                $scope.itens = data.lista;
                $scope.bigTotalItems = data.qtdRegistros;               
            });

My view is like this:

<div class="form-group">
                <label for="inputEmail3"  class="col-sm-3 control-label "><spring:message code="produto.classificacaoProduto"/></label>
                <div class="col-sm-4">
                    <select
                         ng-model="produto.classificacaoProduto.nome" 
                         ng-options="item.value as item for item in [undefinded,{{produto.classificacaoProduto.nome}}]" class="form-control">                                   
                    </select>   
                </div>
            </div>

1 answer

2


I believe the problem with your example is in ng-options, try to replace with:

ng-options="item as item.value for item in itens"

The following is an example that works in a more simplified scenario that you can base:

<div ng-app>
    <script>
        function MyController($scope) {
            $scope.colors = [
              { name: 'black', shade: 'dark' },
              { name: 'white', shade: 'light' },
              { name: 'red', shade: 'dark' },
              { name: 'blue', shade: 'dark' },
              { name: 'yellow', shade: 'light' }
            ];
        }
    </script>

    <div ng-controller="MyController">
        <select ng-model="selectedItem"
                ng-options="color as color.name for color in colors"
                class="form-control">
        </select>
    </div>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.