0

我有以下json:

  [
{
    "normal" :{
        "font": "Burlington Script.ttf",
        "fontFamily": "sf_burlington_scriptregular",
        "fontName": "Burlington Script"
    },
    "bold" : {
        "font": "SF_Burlington_Script_Bold.ttf",
        "fontFamily": "sf_burlington_scriptbold",
        "fontName": "Burlington Script"
    },
    "italic" : {
        "font": "SF_Burlington_Script_Italic.ttf",
        "fontFamily": "sf_burlington_scriptitalic",
        "fontName": "Burlington Script"
    },
    "bold-italic": {
        "font": "SF_Burlington_Script_Bold_Italic.ttf",
        "fontFamily": "sf_burlington_scriptBdIt",
        "fontName": "Burlington Script"
    }

},

{
    "normal" :{
        "font": "Some_Script.ttf",
        "fontFamily": "Some_scriptregular",
        "fontName" : "Some Script"
    },
    "bold" : {
        "font": "Some_Script_Bold.ttf",
        "fontFamily": "Some_scriptbold",
        "fontName" : "Some Script"
    },
    "italic" : {
        "font": "Some_Script_Italic.ttf",
        "fontFamily": "Some_scriptitalic",
        "fontName" : "Some Script"
    },
    "bold-italic": {
        "font": "Some_Script_Bold_Italic.ttf",
        "fontFamily": "Some_scriptBdIt",
        "fontName" : "Some Script"
    }

}
]

我想要做的是仅在“正常”下的下拉列表中显示 fontName,并将值设为 fontFamily。

我试过了

 <select                
      ng-model="selectedFont"
      ng-options="fonts as fonts.normal.fontName for fonts in designFonts" required>
 </select>

但没有运气。我在我的控制器中将 $scope.designFonts 设置为 json 是什么。

4

1 回答 1

2

您可以利用ng-option 表达式select as的一部分来设置在选择选项时设置 ng-model 的值所需的内容。因此,并使用 track by 将选项值设置为相应的值(track by 始终应用于 value)。fonts.normal.fontFamily as fonts.normal.fontName for fonts in designFonts

尝试:-

  <select                
     ng-model="selectedFont"
     ng-options="font as font.normal.fontName for font 
                   in designFonts track by font.normal.fontFamily" 
  required></select>

演示

如果您使用font.normal.fontFamily as font.normal.fontName将 ng-model 设置为相应的 fontFamily,如果您需要将整个对象作为 ng-model,那么请使用您现在的方式。


好吧,可能存在一个 ng-select 错误,您不能在同一字段上一起使用select aswith track by,这会导致选择问题,尽管已ng-model应用。所以你可以使用: -

 ng-options="font as font.normal.fontName for font 
                   in designFonts track by font.normal.fontFamily" 

但你不能

 ng-options="font.normal.fontFamily as font.normal.fontName for font 
                   in designFonts track by font.normal.fontFamily" 
于 2014-10-15T00:32:46.420 回答