簡體   English   中英

選擇選項內的角度ng-switch

[英]Angular ng-switch inside select option

我希望將[數據演示文稿格式]應用於[下拉框]。 我需要在下拉框中顯示“父”,但是我需要使用[tab]顯示“child”,以顯示父子關系。

$ scope.itemlist將按順序准備(即parenta,childa1,childa2,parentb,childb1,childb2)

HTML:

<div ng-controller="MyCtrl">
<h1>-- Data Presentation Format --</h1>
<ul>
    <li ng-repeat="item in itemlist">
        <div ng-switch on="item[1]">
            <div ng-switch-when="Parent">{{item[0]}}</div>
            <div ng-switch-default>&emsp;{{item[0]}}</div>
        </div>    
    </li>
</ul> 
<br/>
<h1>-- Dropdown Box --</h1>
<select ng-model="loc1" ng-options="item[0] for item in itemlist">
    <option value="">Item</option>
</select>
<br/><br/>
<h1>-- What I Got --</h1>
<select ng-model="loc2">
    <option ng-repeat="item in itemlist">
        <div ng-switch on="item[1]">
            <div ng-switch-when="Parent">{{item[0]}}</div>
            <div ng-switch-default>&emsp;{{item[0]}}</div>
        </div>    
    </option>
</select>

javascript:

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
$scope.itemlist =
    [
    ["Unit A", "Parent"], 
    ["Room A", "Child"], 
    ["Room B", "Child"], 
    ["Room C", "Child"], 
    ["Room D", "Child"], 
    ["Room E", "Child"], 
    ["Unit 1", "Parent"],
    ["Room 1", "Child"], 
    ["Room 2", "Child"], 
    ["Room 3", "Child"]
    ];
}

JsFiddle: http//jsfiddle.net/HB7LU/11174/

我不認為你可以在選項元素中使用,因為它沒有呈現為html。 如果你堅持使用元素而不是像jQuery下拉(基本上是a)之類的東西,那么我建議使用破折號或類似來說明父/子關系。

http://jsfiddle.net/HB7LU/11177/

我使用放在示波器上的格式化功能解決了它

$scope.formattedItem = function(item) {
    return item[1] === 'Parent' ? item[0] : '--' + item[0];


<select ng-model="loc2">
    <option ng-repeat="item in itemlist" >
        {{formattedItem(item)}}
    </option>
</select>

暫無
暫無

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

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