簡體   English   中英

AngularJS ng-options從匹配另一個作用域var的數組中獲取選擇的選項

[英]AngularJS ng-options to get selected option from an array that matches another scope 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的新手,需要幫助。

如果您只想將字符串“ 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;
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM