【问题标题】:How can i do this Data Binding in the HTML file component with Angular and Typescript?如何使用 Angular 和 Typescript 在 HTML 文件组件中执行此数据绑定?
【发布时间】:2019-01-23 15:15:58
【问题描述】:

如何在我的简单 component.html 文件中查看有关变量的信息:cityInfo? 这是 component.html 文件,它是一个非常基本的组件,但我想用 component.ts 文件进行数据绑定。 我该怎么办?

<div>
  <p>
    weather works! {{cityInfo}}
  </p>
</div>

这是我的 component.ts。 使用 getWeather(city),我发出一个 http 请求并返回一个 JSON 文件。

export class WeatherComponent implements OnInit {
  public cityVisual: City;

  constructor(
    private weather: WeatherService,
  ) { }

  ngOnInit() {
  }

  weatherCity(city: string) {
    this.weather.getWeather(city).subscribe(resp => {
      this.cityVisual = resp;
      console.log(this.cityVisual.name);

    });
  }
}

我想在我的 component.html 文件中查看“天气工作!nameCity”输出 我怎样才能做到这一点? 请帮帮我...

【问题讨论】:

    标签: angular typescript data-binding


    【解决方案1】:

    以下是绑定城市名称的方法:

    <div>
      <p>
        weather works! {{cityVisual.name}}
      </p>
    </div>
    

    【讨论】:

    • 但我收到此错误:ERROR TypeError: Cannot read property 'name' of undefined
    • 您在哪里调用该服务?我没有看到那部分代码。未定义意味着服务没有被调用或者服务没有响应值。
    • 在带有 get http 请求的 getWeather 中,我收到一个包含城市信息的 JSON 文件,在这种情况下为名称(字符串)
    • 获取信息后如何更新cityVisual变量?
    • 你的weatherCity函数已经足够好了,它会将值赋给cityVisual。但是,我没有看到您从哪里调用它?
    【解决方案2】:

    你可以尝试获取namecityVisual,只有当它被定义时; 您可以通过在cityVisual 之后添加? 来轻松实现。 您收到此错误,因为 weatherCity 方法是异步的。它cityVisual 在 html 初始化时是未定义的。试试这个文本下的代码。

    <div>
      <p>
        weather works! {{cityVisual?.name}}
      </p>
    </div>
    

    【讨论】:

    • 获取信息后如何更新cityVisual?
    • 您需要在代码中的某处调用this.weatherCity('Some city'),例如在ngOnInit
    • 我在 main.component 中调用了 weatherCity('Some city'),在 main.component 中我通过输入选择了城市,所以我调用了 weatherCity
    • console.log(this.cityVisual) 你看到了什么?在你的方法中。你能显示 HTTP 请求后得到的数据吗?我的意思是你的 JSON
    • 当我获得这些信息时,有一种方法可以更新 cityVisual 变量吗?
    【解决方案3】:

    您的函数weatherCity() 永远不会被调用。这就是为什么您的页面上没有呈现任何内容并且您的控制台返回错误的原因。

    尝试在您的 ngOnInit 块中调用它:

    ngOnInit() {
        this.weatherCity('Washington');
    }
    

    或者将其绑定到 HTML 中的按钮:

    <button (click)="this.weatherCity('Washington')">Load Washington</button>
    

    .. 并在浏览器控制台中检查结果。

    如果你在那里得到一个结果,你可以通过使用绑定到那个结果 {{ cityVisual.name }} HTML 中的任何位置。

    确保仅在 cityVisual 是通过将代码放入 *ngIf 语句中实际定义时才呈现名称:

    <p *ngIf="cityVisual">
        Weather works! {{cityVisual.name}}
    </p>
    

    旁注:如果您已完成获取城市的操作,请不要忘记取消订阅您的WeatherService,否则您会出现内存泄漏。

    考虑将订阅存储在一个变量中(例如citySubscription$),这样您就可以在ngOnDestroy 调用.unsubscribe()(或者在您想终止订阅的任何其他时间)

    【讨论】:

    • 调用main.component,通过输入获取城市
    • 像这样:getWeather() { this.weatherComponent.weatherCity(this.city); } }
    • weatherComponent 中的 weatherCity() 函数不返回任何内容。它只在weatherComponent 中设置cityVisual 对象,因此它永远不会进入main.component。更新您的函数,以便返回 WeatherService 调用的结果。然后确保在 main.component 的 getWeather() 函数中使用该结果,例如通过将其设置为 this.result。
    • 也许如果我删除weather.component 并且我只在main.component 中工作是个坏主意?因为情况是这样的:我有调用weather.component的主要组件调用weather.service来获取http请求。用这种方式编程好不好?有道理吗?
    • 如果它有效,它就有效,但它有点违背了您的服务目的。如果我是您,我确实会将服务导入您的主组件,然后将数据提供给您的子组件
    猜你喜欢
    • 2016-10-28
    • 2019-10-19
    • 2018-02-02
    • 2015-08-10
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2022-11-02
    相关资源
    最近更新 更多