将OBJECT ARRAY绑定到AngularJs中的<ul>

时间:2016-06-02 22:06:56

标签: angularjs

我正在从数组中填充一个下拉按钮。正如您在图片中看到的那样显示整个键值对。

1)我只需要显示Label的值。

2)我想拥有&#34;所有地点&#34;作为默认项目。

enter image description here

以下是地点的样子:

Locations:Array[2]
0:Object
$$hashKey:"object:772"
Id:1600
Label:"California"
__proto__:Object
1:Object
$$hashKey:"object:773"
Id:1600
Label:"Atlanta"
__proto__:Object

HTML:

<div class="btn-group btn-toolbar btn-margin-left" style="float: right;">
                        <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                            ALL LOCATIONS {{selectedItem}}
                            <span class="caret"></span>
                        </button>

                        <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                            <li ng-repeat="Label in Summary.Locations">
                                <a ng-click="dropboxitemselected(Label)">
                                    {{Label}}</a>
                            </li>
                        </ul>
</div>

js:

WotcDashBoardModuleService.GetDashBoardSummary(Ein).then(function (response) {
        $scope.Summary = response.data.WotcSummary;

        $scope.selectedItem;
        $scope.dropboxitemselected = function (item) {

            $scope.selectedItem = item;
            //alert($scope.selectedItem);
        }
        console.log($scope.Summary);
    });

2 个答案:

答案 0 :(得分:1)

Summary.Locations是您的对象数组,因此Label会单独绑定到每个元素。这意味着您需要显示{{Label.Label}}而不仅仅是{{Label}},并且您会看到名称。

答案 1 :(得分:1)

这部分:

ALL LOCATIONS {{selectedItem}}

应替换为:

<span ng-if="!selectedItem">ALL LOCATIONS</span>
<span ng-if="selectedItem" ng-bind="selectedItem.Label"></span>

或者用:

<span>{{ selectedItem ? selectedItem.Label : 'ALL LOCATIONS' }}</span>