0

我有一个数组和一个 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

我想要一个基于值的带有默认选择的下拉选择$scope.feedEditor.ft

HTML:

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

我是 AngularJS 的新手,需要一些帮助...

4

1 回答 1

1

如果您只想将字符串“MQ Message”(或value用户选择的任何内容)作为您的 ng-model 值,那么它非常简单:

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

如果您希望完整的对象作为 ng-model 值,那么您必须在 JS 中找到正确的对象。它必须引用/严格等于选项中的那个。

<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;
  }
});
于 2015-12-10T01:32:41.083 回答