首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >angular 7中的ESRI加载器搜索小部件焦点问题

angular 7中的ESRI加载器搜索小部件焦点问题
EN

Stack Overflow用户
提问于 2020-07-14 15:17:39
回答 1查看 347关注 0票数 3

我必须在angular应用程序中使用esri-loader实现ESRI映射。一切正常,但如果我们在搜索窗口小部件中键入内容,然后将焦点移出搜索窗口小部件,则会抛出以下错误

代码语言:javascript
复制
"Uncaught TypeError: Cannot read property 'parentNode' of null
    at HTMLInputElement.d (dojo.js:2209)
    at Object.trigger (jquery.min.js:2)
    at Object.simulate (jquery.min.js:2)
    at HTMLDocument.n (jquery.min.js:2)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
    at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:498)
    at invokeTask (zone.js:1693)
    at HTMLDocument.globalZoneAwareCaptureCallback (zone.js:1762)

我使用的是4.14版本的esri

我已经提交了this,但无法解析。

package.json:

代码语言:javascript
复制
"esri-loader": "^2.15.0"

代码:

代码语言:javascript
复制
import { Component, OnInit, OnDestroy, ViewChild, ElementRef } from "@angular/core";
import { loadModules ,loadScript} from "esri-loader";    
@Component({
    selector: "app-esri-map",
    template: `
  <span> map component workes</span>
  <div class="esri-view" #mapViewNode></div>
  `,
    styles: [`
    @import url('https://js.arcgis.com/4.14/esri/css/main.css');    
    .esri-view {
      height: 600px;
    }`
    ]
})
export class MapComponent implements OnInit, OnDestroy {
    // The <div> where we will place the map
    @ViewChild("mapViewNode", { static: true }) private mapViewEl: ElementRef;
    view: any;    
    constructor() {
         loadScript();
     }    
    async initializeMap() {
        try {
           
            // Load the modules for the ArcGIS API for JavaScript
            const options = { version: '4.14', css: true };
          
            const [Map, MapView, Search] = await loadModules(
                ["esri/Map",
                    "esri/views/MapView",
                    "esri/widgets/Search"],options);

            // Configure the Map
            const mapProperties = {
                basemap: "streets"
            };

            const map = new Map(mapProperties);

            // Initialize the MapView
            const mapViewProperties = {
                container: this.mapViewEl.nativeElement,
                zoom: 10,
                map: map
            };

            this.view = new MapView(mapViewProperties);
            var search = new Search({
                view: this.view,
                searchTerm: `15 Inverness Way East, Englewood, CO80112, United States`,
            });
            console.log('search', search.search());
            this.view.ui.add(search, "top-right");
            search.on("select-result", function (event) {
                console.log("The selected search result: ", event);
                console.log('attributes', event.result.feature.attributes)
            });
            await this.view.when(); // wait for map to load
            console.log('this.view', this.view);
            return this.view;
        } catch (error) {
            console.error("EsriLoader: ", error);
        }
    }

    ngOnInit() {
        this.initializeMap();
    }
    ngAfterViewInit() {

    }

    ngOnDestroy() {
        if (this.view) {
            // destroy the map view
            this.view.container = null;
        }
    }
}

我需要更改一些配置吗?请推荐我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-14 13:14:20

将以下代码添加到initializeMap()中,这样我们就可以显式地将焦点设置为空

代码语言:javascript
复制
const handler = search.on('search-focus', event => {
                handler.remove();
                const input = document.querySelector('.esri-search__input') as any
                if (input) {
                    input.onfocusout = null;
                }
            });

完整代码块

代码语言:javascript
复制
 import { Component, OnInit, OnDestroy, ViewChild, ElementRef, Input, Output, EventEmitter } from "@angular/core";
    import { loadModules, loadScript } from "esri-loader";
    @Component({
        selector: "app-esri-map",
        template: `
      <div class="esri-view" #mapViewNode></div>
      `,
        styles: [`
     /* @import url('https://js.arcgis.com/4.14/esri/css/main.css'); */
        .esri-view {
          height: 400px;
        }`
        ]
    })
    export class MapComponent implements OnInit, OnDestroy {
        // The <div> where we will place the map
        @ViewChild("mapViewNode", { static: true }) private mapViewEl: ElementRef;
        view: any;
        @Input() searchString: string;
        @Output() notifyAttributes: EventEmitter<any> = new EventEmitter();
        constructor() {
            //   loadScript();
            //"esri-loader": "^2.15.0", in dependency section.
        }
        async initializeMap() {
            try {
                // Load the modules for the ArcGIS API for JavaScript
                const options = { version: '4.14', css: true };
                const [Map, MapView, Search] = await loadModules(
                    ["esri/Map",
                        "esri/views/MapView",
                        "esri/widgets/Search"], options);
                // Configure the Map
                const mapProperties = {
                    basemap: "streets"
                };
                const map = new Map(mapProperties);
                // Initialize the MapView
                const mapViewProperties = {
                    container: this.mapViewEl.nativeElement,
                    zoom: 2,
                    center: [0.0000, 45.00000],
                    map: map
                };
                this.view = new MapView(mapViewProperties);
                var search = new Search({
                    view: this.view,
                    searchTerm: this.searchString,
                });
                if (this.searchString) {
                    search.search();
                }
                this.view.ui.add(search, "top-right");
                const handler = search.on('search-focus', event => {
                    handler.remove();
                    const input = document.querySelector('.esri-search__input') as any
                    if (input) {
                        input.onfocusout = null;
                    }
                });
                search.on("select-result", (event) => {
                    this.notifyAttributes.emit(event.result.feature.attributes);
                });
                await this.view.when(() => {
                    const input = document.querySelector('.esri-search__input') as any
                    if (input) {
                        input.onfocusout = null;
                    }
                }); // wait for map to load
                return this.view;
            } catch (error) {
                console.error("EsriLoader: ", error);
            }
        }
        ngOnInit() {
            this.initializeMap();
        }
        ngOnDestroy() {
            if (this.view) {
                this.view.container = null;
            }
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62889912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档