0

I have this problem: i created a selects inside a ng-repeat, the ng-model of each select is dinamically generated.

I need to set a default value to these selects, but i don't know how i can do this

HTML

<div ng-controller="ricercaAttivita">
    <div class="accordion-group" ng-repeat="masterAttribute in masterAttributes">
        <select class="trip dark" ng-change = "search(1, true, true)" ng-model="masterAttribute.Id" ng-options="attr.Id as attr.Value for attr in masterAttribute.Values">
            <option value="">Tutte</option>
        </select>
    </div>
</div>

APP.JS

var tantoSvagoApp = angular.module('tantoSvagoApp');

tantoSvagoApp.controller("ricercaAttivita", function ($scope) {

    $scope.masterAttributes = {"id" : "11", nome" : "MARCHE", "values" :
                                                    [{ "id": "114", "nome": "ANCONA" }, { "id": "116", "nome": "ASCOLI PICENO" }]           
        },
                              {"id" : "12", nome" : "LOMBARDIA", "values" :
                                                    [{ "id": "120", "nome": "MILANO" }, { "id": "121", "nome": "BERGAMO" }]         
        };

});

The ng-model of my select is "masterAttribute.Id", i need to loop every generated select and, in certain conditions, set a particular default option selected.

Something like

$scope.masterAttribute.Id = value;

How i can do this? Anyone can hel me PLEASE?

2 Answers 2

0

just set the selected value object to $scope.masterAttribute. In your controller add

$scope.masterAttribute = masterAttribute.Values[0];

hope this will help.

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

1 Comment

In this case i have two selects, one with ng-model "masterAttribute.Id" = 11 and another one with "masterAttribute.Id" = 12. In this way how can i set a value to the first select and another value to the second select?
0

$scope.masterAttributes should be an array [],

 $scope.masterAttributes=[{
        "id": "11",
        nome " : "
        MARCHE ", "
        values " : [{
            "id": "114",
            "nome": "ANCONA"
        }, {
            "id": "116",
            "nome": "ASCOLI PICENO"
        }]
    },
    {
        "id": "12",
        nome " : "
        LOMBARDIA ", "
        values " : [{
            "id": "120",
            "nome": "MILANO"
        }, {
            "id": "121",
            "nome": "BERGAMO"
        }]

And finally set default value in select is,

$scope.masterAttribute = $scope.masterAttributes[0].values[0];

1 Comment

i have two selects, one with ng-model "masterAttribute.Id" = 11 and another one with "masterAttribute.Id" = 12. In this way how can i set a value to the first select and another value to the second select inside a loop?

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.