Return PopupTemplate 要素图层中的所有属性(字段) - ArcGis Javascript

Return all the attributes(fields) in a feature layer for a PopupTemplate - ArcGis Javascript

有没有办法 return PopupTemplate 要素图层中的所有属性(字段),而无需在 Angular 的 fieldInfos 对象中声明所有属性(字段)?

.ts

const template = {
        title: "{NAME} in {COUNTY}",
        content: ,
        fieldInfos : fieldInfos
      };
      
              
     const layer = new FeatureLayer({
      url: this.featureLayerUrl,
      visible: true,
      popupTemplate : template
      });
      
      var fieldInfos = layer.map(layer.fields, function(field){
         return {
                "fieldName": field.name,
                "label": field.alias,
                "visible": true

    webmap.add(layer);
    

.html

    <!-- Map Div -->

我正在使用 arcgis-js-api 版本 4.2.1.
但是当我使用这个例子时它正在工作。 (但我想动态设置这些字段。)

const fields = [{
  name: "NAME",
  alias: "Name",
  type: "string"
    }, {
  name: "County",
  alias: "County",
  type: "string"
}, {
  
const config = {
  fields: fields,
  title: "County land"
}; 

如果你只是想创建默认的弹出模板(table所有字段),那么你只需要使用FeatureLayer函数createPopupTemplate,它会做所有的事情为你 (ArcGIS JS API - FeatureLayer createPopupTemplate).

现在如果你想做一些额外的配置,你可以使用相同的方法或 popupUtils 方法,它有一个额外的参数。

这是我为您制作的示例,用于展示可能的用途,其中我所做的只是更改 alias 值以使其看起来更好。

<!DOCTYPE html>
<html>

<head>
    <meta charset='utf-8' />
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <title>FeatureLayer field and popup example - 4.21</title>

    <link rel='stylesheet' href='https://js.arcgis.com/4.21/esri/themes/light/main.css' />
    <script src='https://js.arcgis.com/4.21/'></script>

    <style>
        html,
        body,
        #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }
    </style>
    <script>
        require([
            'esri/views/MapView',
            'esri/Map',
            'esri/layers/FeatureLayer',
            'esri/support/popupUtils'
        ], function (MapView, Map, FeatureLayer, popupUtils) {
            const layer1 = new FeatureLayer({
                url: 'https://services9.arcgis.com/RHVPKKiFTONKtxq3/arcgis/rest/services/USGS_Seismic_Data_v1/FeatureServer/1'
            });
            const layer2 = new FeatureLayer({
                portalItem: {
                    id: "f9e348953b3848ec8b69964d5bceae02"
                }
            });
            const layersToCreateMyPopupTemplate = [layer2, layer1];
            const map = new Map({
                basemap: 'gray-vector',
                layers: layersToCreateMyPopupTemplate
            });

            const view = new MapView({
                map: map,
                container: 'viewDiv',
                center: [-98, 40],
                zoom: 4
            });
            const toNiceName = function(text) {
                if (!text) {
                    return null;
                }
                return text
                    .toLowerCase()
                    .split(/_|__|\s/)
                    .join(' ');
            };
            const createMyPopupTemplate = function(layer) {
                console.log(layer.fields); // layer fields
                const config = {
                    fields: layer.fields.map(field => (
                        {
                            name: field.name,
                            type: field.type,
                            alias: toNiceName(field.alias)
                        }
                    )),
                    title: toNiceName(layer.title)
                };
                console.log(config); // config parameter
                return popupUtils.createPopupTemplate(config);
            }
            for (const layer of layersToCreateMyPopupTemplate) {
                view.whenLayerView(layer).then(function (layerView) {
                    const popupTemplate = createMyPopupTemplate(layer)
                    console.log(popupTemplate); // popup template
                    if (!popupTemplate) {
                        console.log("FeatureLayer has no fields.")
                    } else {
                        layer.popupTemplate = popupTemplate;
                    }
                });
            }
        });
    </script>
</head>

<body>
    <div id='viewDiv'></div>
</body>

</html>

示例中 config 变量的 fields 属性 自动转换为 esri/layers/support/Field, wich is different that the fieldInfos property of PopupTemplate that is of type FieldInfo.

的实例