将Angular(javascript)表达式转换为对象数组

Convert Angular (javascript) expression into object array

本文关键字:转换 对象 数组 表达式 Angular javascript      更新时间:2023-09-26

你能告诉我如何将下面提到的角度下拉框表达式转换为对象数组吗?B’因为我必须使用对象数组,而不是下面提到的带有下拉指令的角度表达式。提前谢谢。

a.id as a.num + ', '+ a.townName for a in vm.schoolDistricts

这是指令:Typeahead Dropdown

正如@Grundy和我在评论中指出的,该指令只能从数组中获取任何属性。因此,为了克服这一点,您可以包含您想要的表达式

a.num + ', '+ a.townName

在每个阵列元素中。

如果你当前的数组看起来像

schoolDistrict = [{name:"ABC", value: "ABCVal", num: "123", townName: "myTown"}, ....]

然后它需要改变为类似的东西

schoolDistrict = [{name:"ABC", value: "ABCVal", num: "123", townName: "myTown", typeAheadLabel:"123, myTown"}, ....]

有了这个,你就可以在配置中使用typeAheadLabel,比如

$scope.config = {
        modelLabel:'districts',
        optionLabel:'typeAheadLabel'
    };

在视图中,您可以使用类似的东西

<typeahead-dropdownng-model="vm.property.schoolDistrictId" class="form-control" 
      options="vm.schoolDistricts" config="config" required> 
<option value="" disabled="">-- Select a School District --</option> </typeahead-dropdown>