angularjs select ng-options default value with another array

Issue

I want to set an init option to a select field in angularjs. My problem is that my options came from an array and my default option should be from another array. Here’s my code:

<select ng-model="userorg.organizations" class="form-control" name="organizations" ng-options="o.name for o in org">
  <option value="{{ org._links.parent.href }}">{{ own.name }}</option>
</select>

and in my js file:

$http.get('api/identity/organizations/').success(function(data) {
    $scope.org = data._embedded.organizations;
})

$http.get('user').success(function(data) {
    $scope.own = data.organization;
})

I hope you know what my problem is.

Solution

i created a workaround for my problem

js file:

    $http.get('user').success(function(data) {
    $scope.ownOrg = data.organization.name;
})

$scope.add = function(){
    // Check witch Organization is chosen
    // ***********************************
    if($scope.data.organizations) {
        $scope.data.organizations = $scope.data.organizations;
        var link = $scope.data.organizations._links.self.href.replace("/uaa/", '/');
    } else {
        console.log($scope.user);
        var link = $scope.users[0]._links.organization.href.replace("/uaa/", '/');
    } ...

and in html

 <select ng-model="data.organizations" class="form-control" name="organizations" ng-options="o.name for o in org">
                                    <option value="">{{ ownOrg }}</option>
                                </select>

this works fine for me!

Answered By – Markus Streicher

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published