How to do a select using Angularjs?

1

I'm trying to make a select field in my view and I'm using the framework Angular.js, but I'm not used to working with it and I'm not able to create the function in controller . Here is an example of the code I tried to make that did not 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 looks 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>
    
asked by anonymous 20.05.2014 / 18:29

1 answer

2

I think the problem with your example is in ng-options , try replacing it with:

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

Here is an example that works in a simpler 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>
    
20.05.2014 / 20:02