简体   繁体   English

无法在前端的角度2中获取组件变量值

[英]unable to get component variable value in angular 2 in frontend

Am unable to display mapLoc value in front end... any help? 无法在前端显示mapLoc值...有帮助吗?

Here is my component.ts file 这是我的component.ts文件

export class mycomponent{

 mapLoc:any;

 constructor(){...}

openImageModal(lat,lng){
 this.mapLoc = '';
 var latlng = new google.maps.LatLng(lat, lng);
        var geocoder = geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'latLng': latlng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        console.log(results[1].formatted_address); //able to console
                        this.mapLoc = results[1].formatted_address;
                    }
                }
            });
}

this is mycomponent.html file 这是mycomponent.html文件

<ngui-map zoom="{{zoom}}" center="{{lat}}, {{lng}}" (mapReady$)="onMapReady($event,lat,lng)" (mapClick)="onMapClick($event)" (idle)="onIdle($event)">
                <marker *ngFor="let pos of position" [position]="[pos.lat, pos.lng]" draggable="true" (initialized$)="onMarkerInit($event)"></marker>
                    <info-window id="iw">
                        <div *ngIf="marker.display">
                        {{mapLoc}}
                        </div>
                    </info-window>
            </ngui-map>

The value of this can be different inside the callback (To check this, try console.log("this", this) inside the callback.) . this可以在回调中不同的(要进行检查,尝试console.log("this", this)回调里面)。 Try using a big arrow function instead to keep reference to your component. 尝试改用大箭头功能,以保持对组件的引用。

    geocoder.geocode({ 'latLng': latlng }, (results, status) => {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                    this.mapLoc = results[1].formatted_address;
                }
            }
        });

or cache a reference and use it in the old way: 或缓存引用并以旧方式使用它:

    let component = this;
    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                    console.log(results[1].formatted_address); //able to console
                    component.mapLoc = results[1].formatted_address;
                }
            }
        });

That is very common issue of scoping : 这是范围界定的非常普遍的问题:

Just change : 只是改变 :

geocoder.geocode({ 'latLng': latlng }, function (results, status) {

to

geocoder.geocode({ 'latLng': latlng }, (results, status) => {

OR 要么

geocoder.geocode({ 'latLng': latlng }, function (results, status) { ... }).bind(this)

For More Detail Ref : https://stackoverflow.com/a/47278161/2349407 有关更多详细信息,请参考: https : //stackoverflow.com/a/47278161/2349407

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

相关问题 Angular:如何从一个组件的前端(app.compont.html)获取值到另一个组件的后端(other.component.ts) - Angular: How to get value from one component's frontend (app.compont.html) to another component's backend (other.component.ts) 如何将Angular 2 Frontend Express JS变量导出到Angular 2组件? - How can I export Angular 2 Frontend Express JS variable to a Angular 2 component? 无法在角度组件中设置bound属性的值 - Unable to set the value of bound property in angular component 无法以角度获取构造函数中的值 - unable to get the value in the constructor in angular 无法从 angular 中的另一个组件更改一个组件的属性值 - unable to change an attribute value of one component from another component in angular 在前端显示 CSS 变量的值 - Displaying value of a CSS Variable in Frontend 无法使用Angular从HTML视图获取值到控制器。 解决方案返回$ scope变量未定义 - Unable to get value from HTML view to controller using Angular. Solution returns $scope variable undefined 角度(jQuery timepicker)无法获取输入值 - angular (jquery timepicker) unable to get value of input 无法获取角度单选按钮的值 - Unable to get value of radio button in angular 无法使用jQuery在角度组件中为表单控件设置值? - unable to set value for a form control in an angular component using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM