0

I have an array and a var

$scope.fcFeedTypes = [{"name":"Plain Text","value":"Plain Text"},{"name":"MQ Message","value":"MQ Message"}];
}
$scope.feedEditor.ft = "MQ Message"; // this is dynamically obtained from some other source

I want a dropdown select with default selection based on the value of $scope.feedEditor.ft

HTML:

<select ng-model="fcFeedTypes"
        ng-options="ft.name for ft in fcFeedTypes"
        ng-init="ft=feedEditor.ft">

I am new to AngularJS and need some help...

Phil
  • 157,677
  • 23
  • 242
  • 245

1 Answers1

1

If you just want the string "MQ Message" (or the value of whatever a user selects) as your ng-model value, then it's quite simple:

<select ng-model="someModelName"
    ng-options="ft.value as ft.name for ft in fcFeedTypes"
    ng-init="someModelName=feedEditor.ft">

If you want the full object as the ng-model value, then you've got to find the right one in JS. It must be referentially/strictly equal to the one in the options.

<select ng-model="someModelName"
    ng-options="ft.name for ft in fcFeedTypes">

-

$scope.fcFeedTypes = [{"name":"Plain Text","value":"Plain Text"},{"name":"MQ Message","value":"MQ Message"}];

$scope.feedEditor.ft = "MQ Message"; // this is dynamically obtained from some other source

angular.forEach($scope.fcFeedTypes, function(feedType){
  if(feedType.value === $scope.feedEditor.ft){
    $scope.someModelName = feedType;
  }
});
HankScorpio
  • 3,612
  • 15
  • 27