简体   繁体   English

AngularJS-在选择的optgroups中按对象显示嵌套的JSON

[英]AngularJS - Show nested JSON by object in select optgroups

I'm trying to create a select with optgroups for countries in the UK, each including a list of their counties that can be selected. 我正在尝试为英国的国家/地区创建带有optgroup的选择,每个国家/地区都包括可以选择的县列表。

I'm trying to do this using ng-repeat with Angular to access the data, which I can do for single-level arrays in a select, but I'm not sure I've set up my counties array correctly to do this, and the counties are obviously not showing in the list. 我正在尝试使用ng-repeat和Angular来访问数据,我可以在一个选择中对单级数组执行此操作,但是我不确定我是否已正确设置县数组来执行此操作,而且县显然不在清单中。

JSFiddle: https://jsfiddle.net/ehtxcwtk/1/ . JSFiddle: https ://jsfiddle.net/ehtxcwtk/1/。

HTML: HTML:

<select data-ng-model="address.county" id="countySelect" material-select watch name="countySelect" class="validate">

    <option value="" selected>Select an option</option>

        <optgroup label="England">

            <option data-ng-repeat="county in counties.england">{{county.name}}</option>

        </optgroup>

        <optgroup label="Scotland">

            <option data-ng-repeat="county in counties.scotland">{{county.name}}</option>

        </optgroup>

        <optgroup label="Wales">

            <option data-ng-repeat="county in counties.wales">{{county.name}}</option>

        </optgroup>

        <optgroup label="Northern Ireland">

            <option data-ng-repeat="county in counties.northernIreland">{{county.name}}</option>

        </optgroup>

    </select>

- --

Array: 数组:

var counties = [
    {
        england: [
            {
                name: 'Bath and North East Somerset'
            },
            {
                name: 'Bedfordshire'
            },
            {
                name: 'Berkshire'
            },
            {
                name: 'Bristol'
            },
            {
                name: 'Buckinghamshire'
            },
            {
                name: 'Cambridgeshire'
            },
            {
                name: 'Cheshire'
            },
            {
                name: 'Cornwall'
            },
            {
                name: 'County Durham'
            },
            {
                name: 'Cumbria'
            },
            {
                name: 'Derbyshire'
            },
            {
                name: 'Devon'
            },
            {
                name: 'Dorset'
            },
            {
                name: 'East Riding of Yorkshire'
            },
            {
                name: 'East Sussex'
            },
            {
                name: 'Gloucestershire'
            },
            {
                name: 'Greater London'
            },
            {
                name: 'Greater Manchester'
            },
            {
                name: 'Hampshire'
            },
            {
                name: 'Herefordshire'
            },
            {
                name: 'Hertfordshire'
            },
            {
                name: 'Isle of Wight'
            },
            {
                name: 'Isles of Scilly'
            },
            {
                name: 'Kent'
            },
            {
                name: 'Lancashire'
            },
            {
                name: 'Leicestershire'
            },
            {
                name: 'Lincolnshire'
            },
            {
                name: 'Merseyside'
            },
            {
                name: 'Norfolk'
            },
            {
                name: 'North Somerset'
            },
            {
                name: 'North Yorkshire'
            },
            {
                name: 'Northamptonshire'
            },
            {
                name: 'Northumberland'
            },
            {
                name: 'Nottinghamshire'
            },
            {
                name: 'Oxfordshire'
            },
            {
                name: 'Rutland'
            },
            {
                name: 'Shropshire'
            },
            {
                name: 'Somerset'
            },
            {
                name: 'South Gloucestershire'
            },
            {
                name: 'South Yorkshire'
            },
            {
                name: 'Staffordshire'
            },
            {
                name: 'Suffolk'
            },
            {
                name: 'Surrey'
            },
            {
                name: 'Tyne and Wear'
            },
            {
                name: 'Warwickshire'
            },
            {
                name: 'West Midlands'
            },
            {
                name: 'West Sussex'
            },
            {
                name: 'West Yorkshire'
            },
            {
                name: 'Wiltshire'
            },
            {
                name: 'Worcestershire'
            }
        ]
    },
    {
        scotland: [
            {
                name: 'Aberdeenshire'
            },
            {
                name: 'Angus'
            },
            {
                name: 'Argyll and Bute'
            },
            {
                name: 'Ayrshire'
            },
            {
                name: 'Banffshire'
            },
            {
                name: 'Berwickshire'
            },
            {
                name: 'Borders'
            },
            {
                name: 'Caithness'
            },
            {
                name: 'Clackmannanshire'
            },
            {
                name: 'Dumfries and Galloway'
            },
            {
                name: 'Dunbartonshire'
            },
            {
                name: 'East Ayrshire'
            },
            {
                name: 'East Dunbartonshire'
            },
            {
                name: 'East Lothian'
            },
            {
                name: 'East Renfrewshire'
            },
            {
                name: 'Fife'
            },
            {
                name: 'Highland'
            },
            {
                name: 'Inverclyde'
            },
            {
                name: 'Kincardineshire'
            },
            {
                name: 'Lanarkshire'
            },
            {
                name: 'Midlothian'
            },
            {
                name: 'Moray'
            },
            {
                name: 'North Ayrshire'
            },
            {
                name: 'North Lanarkshire'
            },
            {
                name: 'Orkney'
            },
            {
                name: 'Perth and Kinross'
            },
            {
                name: 'Renfrewshire'
            },
            {
                name: 'Shetland'
            },
            {
                name: 'South Ayrshire'
            },
            {
                name: 'South Lanarkshire'
            },
            {
                name: 'Stirlingshire'
            },
            {
                name: 'West Dunbartonshire'
            },
            {
                name: 'West Lothian'
            },
            {
                name: 'Western Isles'
            }
        ]
    },
    {
        wales: [
            {
                name: 'Blaenau Gwent'  
            },
            {
                name: 'Bridgend'  
            },
            {
                name: 'Caerphilly'  
            },
            {
                name: 'Cardiff'  
            },
            {
                name: 'Carmarthenshire'  
            },
            {
                name: 'Ceredigion'  
            },
            {
                name: 'Conwy'  
            },
            {
                name: 'Denbighshire'  
            },
            {
                name: 'Isle of Anglesey'  
            },
            {
                name: 'Merthyr Tydfil'  
            },
            {
                name: 'Monmouthshire'  
            },
            {
                name: 'Neath Port Talbot'  
            },
            {
                name: 'Newport'  
            },
            {
                name: 'Pembrokeshire'  
            },
            {
                name: 'Powys'  
            },
            {
                name: 'Rhundda Cynon Taff'  
            },
            {
                name: 'Swansea'  
            },
            {
                name: 'Torfaen'  
            },
            {
                name: 'Vale of Glamorgan'  
            },
            {
                name: 'Wrexham'  
            }
        ],
    },
    {
        scotland: [
            {
                name: 'Antrim'
            },
            {
                name: 'Armagh'
            },
            {
                name: 'Down'
            },
            {
                name: 'Fermanagh'
            },
            {
                name: 'Londonderry'
            },
            {
                name: 'Tyrone'
            },
        ]
    }
];

Your data is structured as an array. 您的数据结构为数组。 Try turning counties into an Object instead, since you're trying to access properties in your template code. 请尝试将counties变成对象,因为您正在尝试访问模板代码中的属性。 Like so: 像这样:

var counties =
    {
        england: [
            {
                name: 'Bath and North East Somerset'
            },
            ...
    }

instead of 代替

var counties = [
    {
        england: [
            {
                name: 'Bath and North East Somerset'
            },
            ...
    }
]

If you can't change the data model, try accessing them in your template like so as a workaround: 如果您无法更改数据模型,请尝试通过以下方式在模板中访问它们:

data-ng-repeat="county in counties[0].scotland"

you made a tiny mistake when addressing/setting up your counties list. 在设置/设置县列表时,您犯了一个小错误。 You are referring to counties.england but in your data structure it is rather counties[0].england. 您指的是counties.england,但是在您的数据结构中,它是counties [0] .england。 Please see this working codepen: https://codepen.io/andrasadam93/pen/XegMJQ 请参阅此工作代码笔: https ://codepen.io/andrasadam93/pen/XegMJQ

The important part is the removed array from the beginning: 重要的部分是从一开始就删除的数组:

$scope.counties = {
        england: [
            {
                name: 'Bath and North East Somerset'
            },
...

In case it needs further clarification please let me know and will add some further detail! 如果需要进一步说明,请告诉我,并将添加更多详细信息!

Your county object is in a wrong format. county object格式错误。 It needs to be an object and it should contain all the counties as objects . 它必须是一个object ,并且应包含所有县作为objects Just look at the following example. 只要看下面的例子。 I corrected your object to work properly. 我更正了您的对象以使其正常工作。

 var app = angular.module("app", []); app.controller("MainController", function($scope) { //Correct county object format ////////////////////////////// $scope.counties = { england: [{ name: 'Bath and North East Somerset' }, { name: 'Bedfordshire' }, { name: 'Berkshire' }, { name: 'Bristol' }, { name: 'Buckinghamshire' }, { name: 'Cambridgeshire' }, { name: 'Cheshire' }, { name: 'Cornwall' }, { name: 'County Durham' }, { name: 'Cumbria' }, { name: 'Derbyshire' }, { name: 'Devon' }, { name: 'Dorset' }, { name: 'East Riding of Yorkshire' }, { name: 'East Sussex' }, { name: 'Gloucestershire' }, { name: 'Greater London' }, { name: 'Greater Manchester' }, { name: 'Hampshire' }, { name: 'Herefordshire' }, { name: 'Hertfordshire' }, { name: 'Isle of Wight' }, { name: 'Isles of Scilly' }, { name: 'Kent' }, { name: 'Lancashire' }, { name: 'Leicestershire' }, { name: 'Lincolnshire' }, { name: 'Merseyside' }, { name: 'Norfolk' }, { name: 'North Somerset' }, { name: 'North Yorkshire' }, { name: 'Northamptonshire' }, { name: 'Northumberland' }, { name: 'Nottinghamshire' }, { name: 'Oxfordshire' }, { name: 'Rutland' }, { name: 'Shropshire' }, { name: 'Somerset' }, { name: 'South Gloucestershire' }, { name: 'South Yorkshire' }, { name: 'Staffordshire' }, { name: 'Suffolk' }, { name: 'Surrey' }, { name: 'Tyne and Wear' }, { name: 'Warwickshire' }, { name: 'West Midlands' }, { name: 'West Sussex' }, { name: 'West Yorkshire' }, { name: 'Wiltshire' }, { name: 'Worcestershire' } ], scotland: [{ name: 'Aberdeenshire' }, { name: 'Angus' }, { name: 'Argyll and Bute' }, { name: 'Ayrshire' }, { name: 'Banffshire' }, { name: 'Berwickshire' }, { name: 'Borders' }, { name: 'Caithness' }, { name: 'Clackmannanshire' }, { name: 'Dumfries and Galloway' }, { name: 'Dunbartonshire' }, { name: 'East Ayrshire' }, { name: 'East Dunbartonshire' }, { name: 'East Lothian' }, { name: 'East Renfrewshire' }, { name: 'Fife' }, { name: 'Highland' }, { name: 'Inverclyde' }, { name: 'Kincardineshire' }, { name: 'Lanarkshire' }, { name: 'Midlothian' }, { name: 'Moray' }, { name: 'North Ayrshire' }, { name: 'North Lanarkshire' }, { name: 'Orkney' }, { name: 'Perth and Kinross' }, { name: 'Renfrewshire' }, { name: 'Shetland' }, { name: 'South Ayrshire' }, { name: 'South Lanarkshire' }, { name: 'Stirlingshire' }, { name: 'West Dunbartonshire' }, { name: 'West Lothian' }, { name: 'Western Isles' } ], wales: [{ name: 'Blaenau Gwent' }, { name: 'Bridgend' }, { name: 'Caerphilly' }, { name: 'Cardiff' }, { name: 'Carmarthenshire' }, { name: 'Ceredigion' }, { name: 'Conwy' }, { name: 'Denbighshire' }, { name: 'Isle of Anglesey' }, { name: 'Merthyr Tydfil' }, { name: 'Monmouthshire' }, { name: 'Neath Port Talbot' }, { name: 'Newport' }, { name: 'Pembrokeshire' }, { name: 'Powys' }, { name: 'Rhundda Cynon Taff' }, { name: 'Swansea' }, { name: 'Torfaen' }, { name: 'Vale of Glamorgan' }, { name: 'Wrexham' } ], scotland: [{ name: 'Antrim' }, { name: 'Armagh' }, { name: 'Down' }, { name: 'Fermanagh' }, { name: 'Londonderry' }, { name: 'Tyrone' }, ] }; //Default county $scope.address = { county: "Bristol" } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="MainController"> <select ng-model="address.county" id="countySelect" material-select watch name="countySelect" class="validate"> <option value="" selected>Select an option</option> <optgroup label="England"> <option data-ng-repeat="county in counties.england">{{county.name}}</option> </optgroup> <optgroup label="Scotland"> <option data-ng-repeat="county in counties.scotland">{{county.name}}</option> </optgroup> <optgroup label="Wales"> <option data-ng-repeat="county in counties.wales">{{county.name}}</option> </optgroup> <optgroup label="Northern Ireland"> <option data-ng-repeat="county in counties.northernIreland">{{county.name}}</option> </optgroup> </select> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM