Suraj
Suraj

Reputation: 586

can not set drop-down option as a default selected

i'm trying to set drop-down value as default from javascript code but i cannot set it.

my code jsFiddle

<div ng-controller="csrClrt">

    <div ng:repeat="(key, item) in items track by $index">

        <md-input-container>
            <label>Face Style:</label>
            <md-select ng-model="item.style" ng-change="f_style()" ng-model-options="{trackBy: '$value.id'}">
                <md-option ng-value="user" ng:repeat="user in styles">{{user.name}}</md-option>
            </md-select>
        </md-input-container>
        <div>
            Id:{{item.style.id}}
            <br/> Want to Display Name {{item.style.name}}
        </div>
    </div>


</div>

JavaScript Code

angular.module('MyApp', ['ngMaterial'])
.controller('csrClrt', function ($scope) {
    stylesdata = [{
            name: "a",
            id: 0
        }, {
            name: "b",
            id: 1
        }, {
            name: "c",
            id: 3
        }
    ];
    $scope.items = ["0"];
    var style = [];
    for (var i = 0; i < stylesdata.length; i++) {
        style.push({
            name: stylesdata[i].name,
            id: stylesdata[i].id
        })
    }
    $scope.styles = style;
})

Upvotes: 0

Views: 41

Answers (1)

Satpal
Satpal

Reputation: 133403

You have not create items object properly and no need of ng:repeat="(key, item) in items track by $index"

View

<div ng-controller="csrClrt">
    <md-input-container>
        <label>Face Style:</label>
        <md-select ng-model="items.style" ng-change="f_style()" ng-model-options="{trackBy: '$value.id'}">
            <md-option ng-value="user" ng:repeat="user in styles">{{user.name}}</md-option>
        </md-select>
    </md-input-container>
    <div>
        {{items.style}}
    </div>
</div>

Controller

$scope.items = {
    style: {
        id: 1
    }
};

Updated Fiddle

Upvotes: 1

Related Questions