How to make a datalist of options generated with a json using Angularjs?

Asked

Viewed 133 times

0

I wanted that when filling the field of professions, a list of the professions already registered for the end user would appear, as a way to help the user follow a pattern for the names. So I thought one would use a datalist that showed the professions already registered. I created a json that lists the professions already registered in the table, as well as a controller using Angularjs that pulls this data. Searching, I found two ways to make my datalist, one using the option tag, another using the select tag, the problem is that none works the way I wanted.

When I use the option, it renders as follows: inserir a descrição da imagem aqui

When I use select, it renders like this: inserir a descrição da imagem aqui

Below are my codes:

Controller:

angular.module('myApp').controller('profissoesCtrl', function ($scope, $http) {
$scope.profissoes = [];
var carregarProfissoes = function (){
    $http.get("ListaProfissoes.php").then(function(response){
        $scope.profissoes=response.data;
    });
};
carregarProfissoes();

My html, using the option:

<datalist ng-controller="profissoesCtrl" id="profissoes"> 
		 <option ng-repeat="p in profissoes" value="{{p}}">
	</datalist>

My html using select:

<datalist ng-controller="profissoesCtrl" id="profissoes"> 
	<select class="oculto" ng-model="aluno.profissao" ng-options="p.profissao as p.profissao for p in profissoes" > </select>
	</datalist>

From now on, I thank everyone who answers!

  • try to use response.data.profissao with option, like this: $scope.profissoes=response.data.profissao;

  • I tried to do this but it didn’t work, it returns Undefined when giving a console.log in $Scope.profissoes trying this way.

  • 1

    It was a silly mistake, in the options tag I should have pasted the value with p.professional, then it worked out, anyway, thank you very much

No answers

Browser other questions tagged

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