I'm new in angularjs and I have a problem with values from http request. I use this values to populate an Select field but I can't set the default value (for example the first element of array). This is my javascript code:
var app = angular.module('myApp',[]);
app.controller('freeUserController', function($scope, $http) {
$http({
method: 'GET',
url: 'users'
}).then(function successCallback(response) {
$scope.users = response.data.result;
$scope.selectedItem = $scope.users[0];
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
});
Instead on my html code I have
<label>Username</label> <select class="form-control select2"
style="width: 100%;" name="user" ng-model="selectedItem" ng-options="user.username for user in users">
</select>
If I change the result of http request with static array all works fine. Are you know why I have this problem?
Before I used this thymeleaf code with success:
<div class="form-group" id=existingUser>
<label>Username</label> <select class="form-control select2"
style="width: 100%;" th:field="*{user}">
<option th:each="user: ${users}" th:value="${user.username}"
th:text="${user.username}"></option>
</select>
</div>
this is the debug image
I tought was a problem of plugin, but with static array it works like with thymeleaf

console.log(response.data.result). What is displayed?