Search code examples
htmlangularjsselectng-options

Setting up ng-options with custom index's


I am new to AngularJS and trying to set up a few ng-options which have unique indexes. I have a few tables where I want to set the indexes to the table's Primary Key.

i.e. If I had PK's 1, 3, 4, 5. I would want those indexes only in my select.

I first call the information as such, with the "names" being what the label should be, and the "ID"'s being the index.

var query = from champs in db.championLists
            select new
            { 
              cID = champs.ID, 
              cName = champs.Name
            };

I call and store this information as Champions

championService.getResources().success(function (champs) {
    $scope.Champions = champs;
});

Now I get confused on the part of actually settting up the information within the HTML. Here is my attempt thus far. (and I already tested to make sure the data is actually reaching the View, I just can't figure out how to set my select)

<select ng-options="select as cName for cID in Champions" ng-init="index = 0" ng-model="cID[index]">{{Champions.cName}}</select>

Solution

  • You do not need the "select as" on the ng-options.

    <select ng-options="champ.cID as champ.cName for champ in Champions" ng-init="index = 0" ng-model="cID[index]">{{champ.cName}}</select>