Issue
EDIT : The code its ok, the problem was
materialize
select
, did not work with dynamic angular elements, use the class browser-default instead of input-field and do not inizializate with jquery.
Hello I'm trying to display data in a select
tag with angular
I read a lot but I cannot solve this problem. As you can see with test code works but with the function does not. Need help. Thanks.
This is the JSON that PHP endpoint returns:
{
"records":[
{
"numero":"312312"
},
{
"numero":"31221111"
},
{
"numero":"311241"
},
{
"numero":"112441"
},
{
"numero":"11312"
},
{
"numero":"131"
}
]
}
Controller:
app.controller('chequeoCtrl', function($scope, $http) {
//with this function do not work
$scope.leerNumero = function() {
$http.get("objetos/autoelevador/leer_numero.php").success(function(response) {
$scope.data = response.records;
console.log($scope.data);
});
};
$scope.leerNumero();
// with this array works, just for test!!
/*$scope.names = [{"name":"pepe"},{"name":"pepe2"}];
console.log($scope.nombres); */
})
My select
tag:
<select ng-model="autoelev" ng-options="item.numero as item.numero for item in data">
<option value="" disabled selected>Seleccionar autoelevador</option>
</select>
Solution
Try code below, Demo here:
View:
<body ng-controller="MainCtrl">
<select ng-options="item.numero for item in data" ng-model="chosen">
<option value="" disabled selected>Seleccionar autoelevador</option>
</select>
</body>
Controller:
angular.module('app', [])
.controller('MainCtrl', function($scope, $http) {
$http.get('data.json')
.then(function(res) {
$scope.data = res.data.records;
console.log($scope.data);
});
})
Do $scope.data = response.data.records
in your controller and make sure its an array of options.
console.log($scope.data)
in your controller should print as below:
[{"numero":"312312"},{"numero":"31221111"},{"numero":"311241"},{"numero":"112441"},{"numero":"11312"},{"numero":"131"}
Answered By - Prashant G
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.