简体   繁体   English

IONIC/Angular:HTML 不显示 ts 文件中的信息

[英]IONIC/Angular: HTML doesnt display information from ts file

Im having a problem with showing the information on my html page im sending an object from a "home-page.ts" to "informacion-bar.page.ts" then i show them on html with {{ res?.title }} that works on my another projects but I dont know why now its not working so I need a little help我在我的 html 页面上显示信息时遇到问题,我将一个对象从“home-page.ts”发送到“informacion-bar.page.ts”,然后我用 {{ res?.title }} 在 html 上显示它们这适用于我的另一个项目,但我不知道为什么现在它不起作用所以我需要一点帮助

Sorry for my low level english对不起我的英语水平低

Here i have my typescript file where i get the data and assign the value to "res" and ill use it on my html在这里,我有我的打字稿文件,我在其中获取数据并将值分配给“res”并在我的 html 上使用它

Here is my html where you can see its really simple using ionic components and trying to show information这是我的 html,您可以在其中看到使用离子组件并尝试显示信息非常简单

And my console that show the object with the "title" and "snippet" property我的控制台显示具有“title”和“snippet”属性的对象

console doesnt send me any error just a little warning "Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?"控制台不会向我发送任何错误,只是一个小小的警告“导航在 Angular 区域外触发,您是否忘记调用 'ngZone.run()'?” that i ignore and my result is showing "Information: ' '" just blank and the label is blank too我忽略了,我的结果显示“信息:''”只是空白,标签也是空白

TS TS

export class InformacionBarPage implements OnInit {
  public res: any;
  constructor(public events2: Events) {
    this.events2.subscribe('my-message', (data) => {
      this.res = data;
      console.log(this.res);
      console.log(this.res.snippet);
    });
   }

  ngOnInit() {
  }

}

HTML HTML

<ion-content>
  <ion-list>
    <ion-item>
      <ion-label>
        test: {{ res?.snippet }}
      </ion-label>
    </ion-item>
  </ion-list>
</ion-content>

Console:安慰:

{position: {…}, title: "Bar de prueba 1", snippet: "Billar, Espacioso, Tranquilo", icon: {…}, animation: undefined, …}
animation: undefined
disableAutoPan: false
draggable: false
flat: false
icon: {url: "assets/imgs/marker_bar.png", size: {…}, anchor: Array(2)}
infoWindowAnchor: (2) [16, 0]
noCache: false
opacity: 1
position: {lng: -6.206721, lat: 36.528835}
rotation: 0
snippet: "Billar, Espacioso, Tranquilo"
title: "Bar de prueba 1"
visible: true
zIndex: 0
__proto__: Object

SOLVED i solved it thanks to this angularjs - events-publish-subscribe解决了我解决了这个angularjs - events-publish-subscribe

In resume you need to load and get ready the subscribe on 'page2' before you send data from page1, so you need to subscribe before publish, then you can move to page2, subscribe and then publish data from page1 i'll let you an example在简历中,您需要在从 page1 发送数据之前加载并准备好在 'page2' 上的订阅,因此您需要在发布之前订阅,然后您可以移动到 page2,订阅然后从 page1 发布数据我会让你例子

page 1: navigate to page2, load his constructor THEN "publish"第 1 页:导航到第 2 页,加载他的构造函数然后“发布”

enviarPosicion() {
this.zone.run(() => {
  this.nav.navigateForward('/lista-bares').then(() => {
    this.events1.publish('posicion_usuario', this.posicion);
  });
});

} }

page 2: load page2 and subscribe BEFORE publish, then you load this subscribe and THEN you publish the data as i show you on last code第 2 页:加载 page2 并在发布之前订阅,然后加载此订阅,然后发布数据,就像我在最后一个代码中向您展示的那样

    this.events1.subscribe('posicion_usuario', (pos) => {
    this.lat = pos.lat;
    this.lng = pos.lng;
  });
}

subscribe of page 2 needs to be on constructor so it load when you navigate to page2.订阅第 2 页需要在构造函数上,以便在您导航到第 2 页时加载。

You should instead make the res an observable within the call back by first importing您应该首先导入,使 res 在回调中成为可观察的

import { Observable, of} from 'rxjs';

and then进而

this.events2.subscribe('my-message', (data) => {
  this.res = of(data);
  //
});

and then view it as然后将其视为

{{ (res | async)?.propertyname }}

I'm assuming you are using ionic 4. In previous versions Observables are imported differently.我假设您使用的是 ionic 4。在以前的版本中,Observables 的导入方式不同。

I have faced the same issue, I resolved it using NgZone from @angular/core.我遇到了同样的问题,我使用来自@angular/core 的 NgZone 解决了它。 Don't know if it is the best way but it did work for me.不知道这是否是最好的方法,但它确实对我有用。

import {NgZone} from '@angular/core';

add it in constructor :将其添加到构造函数中:

constructor(private zone: NgZone) {}

wrap the code inside zone like this :将代码包裹在区域内,如下所示:

this.events2.subscribe('my-message', (data) => {
  this.zone.run(() => {
    this.res = data;
  })
});

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

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