0
votes

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

2 Answers

0
votes

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

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

hope this will help.

0
votes

$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];