1

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

debug image

I tought was a problem of plugin, but with static array it works like with thymeleaf

4
  • try it jsfiddle.net/sinkz/HB7LU/23128 Commented Feb 2, 2016 at 10:21
  • the problem is with the http call Commented Feb 2, 2016 at 10:31
  • try console.log(response.data.result). What is displayed? Commented Feb 2, 2016 at 10:41
  • I added browser debug image Commented Feb 2, 2016 at 10:46

5 Answers 5

1

Your using user.username to display and in script your assigning complete user Object like this

$scope.selectedItem = $scope.users[0];

change this to

$scope.selectedItem = $scope.users[0].username;

This should work now

Sign up to request clarification or add additional context in comments.

Comments

0

should can easily give the select an "ng-init" like so:

<select class="form-control select2" ng-init="selectedItem = users[0]"
style="width: 100%;" name="user" ng-model="selectedItem" ng-options="user.username for user in users">

Sorry for formating

Comments

0

Try to set user.id as user.username in ng-options directive.

<select class="form-control select2"
    style="width: 100%;" name="user" ng-model="selectedItem" ng-options="user.id as user.username for user in users">
</select>

Comments

0

In ng-model give like this and try.

ng-model="selectedItem.username"

Otherwise in controller change like below:

$scope.selectedItem=$scope.users[0].username;

2 Comments

Can you post your response? What u r getting from back end?
if I set $scope.users[0].username I must not use selectedItem.username in ng-model. In javascript the variables are setted fine, but the default value of select is empty string
0

Try this

    <select ng-model="selectedItem" ng-options="user.username as user.username for user in users">
          <option value="" selected="selected">{{users[0].m}}</option>
    </select>

Controller

$scope.selectedItem = $scope.users[0].username;

2 Comments

without the class it works, maybe the problem is between Select2 plugin and angularjs
it doesn't work, I can try to check if value is selected and let the default value empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.