简体   繁体   English

如何在Angular 7中使用打字稿获取属性值

[英]How to get value of property using typescript in angular 7

I want to take a value from my property, countOnProgress. 我想从我的属性countOnProgress中获取一个值。 I can get the value of countOnProgress when I subcribe, but outside subscribe countOnProgress return 0 so I cant use countOnProgress at progressLastYear. 我可以在订阅时获得countOnProgress的值,但在外部订阅countOnProgress时返回0,所以我不能在progressLastYear使用countOnProgress。 How to set value of countOnProgress with value from subcribe without return 0 如何使用来自订阅的值设置countOnProgress的值而不返回0

import { Component, OnInit, Inject } from '@angular/core';
import { Router } from '@angular/router';
import { DashboardService } from './dashboard.service';
import { Observable, of, timer } from 'rxjs';
import 'rxjs/add/operator/takeWhile';
import 'rxjs/add/observable/timer';

@Component({
  templateUrl: 'dashboard.component.html',
  styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {

  alive = true;
  countOnProgress:number = 0;
  max: number = 200;
  value: number = 100;
  stacked: any[] = [];

  constructor(@Inject (DashboardService) private dashboardService: DashboardService){ 
  }

  ngOnInit(): void {
    Observable.timer(0, 30000)
    .takeWhile(() => this.alive)
    .subscribe(() => {
      this.dashboardService.getCountProgress().subscribe(resp => {
        this.countOnProgress = resp.d;
        console.log(this.countOnProgress); //It found the data
      })
    });
    this.progressLastYear();
  }

  progressLastYear(): void{
    const types = ['success', 'info', 'warning', 'danger'];
    const values = [this.countOnProgress];
    console.log(values);
    this.stacked = [];
    for (let i = 0; i < 5; i++) {
      this.stacked.push({
        value: values[1],
        type: types[i],
        label: values[1]
      });
      console.log(this.stacked); //The datas: 0, succes, 0 (didnt get countOnProgress' value)
    }
  }
}

Thank you 谢谢

Refactor your code like below : 重构您的代码,如下所示:

ngOnInit(): void {
    Observable.timer(0, 30000)
    .takeWhile(() => this.alive)
    .subscribe(() => {
      this.dashboardService.getCountProgress().subscribe(resp => {
        this.countOnProgress = resp.d;
        console.log(this.countOnProgress); //It found the data
        this.progressLastYear();   // < -- move the function call here
       })
    });

  }

Why My code is not working ? 为什么我的代码不起作用?

JS is Asynchronous hence it doesn't wait for any I/O request to get complete and keeps executing next lines of code. JS是异步的,因此它不等待任何I / O请求完成,而是继续执行下一行代码。

In you code both Observable.timer(0, 30000) and this.dashboardService.getCountProgress() is asynchronous . 在您的代码中, Observable.timer(0, 30000)this.dashboardService.getCountProgress()都是异步的。 Hence while executing JS will not wait for them to get complete and keep executing the next line of code. 因此,在执行JS时不会等待它们完成并继续执行下一行代码。 As a result the method call this.progressLastYear() got called before the completion of the service call . 结果,在服务调用完成之前调用了方法this.progressLastYear() Hence You were not getting the value of countOnProgress . 因此,您没有得到countOnProgress的值。

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

相关问题 如何使用 angular 2 中的打字稿从文本框中获取值 - how to get value from textbox using typescript in angular 2 使用角度/打字稿中的另一个函数为接口属性赋值? - Assign a value to an interface property using a function on another in angular / typescript? 如何将属性值(如果不为null)分配给TypeScript,Angular中的对象实例 - How to assign property value if not null, to object instance in TypeScript, Angular 如何访问或读取 angular typescript 文件中的 css 属性值 - How to access or read the css property value in angular typescript file 如何从类属性TypeScript - Angular中侦听值更改 - How to listen for value changes from class property TypeScript - Angular Angular/Typescript - 如何根据属性从对象中获取新列表? - Angular/Typescript - how to get a new list from objects depending on property? 如何使用Angular 2或TypeScript获取访客位置 - How to get visitors location using Angular 2 or TypeScript 如何使用角度和打字稿循环innerHTML属性绑定? - How to loop innerHTML property binding using angular and typescript? 如何使用 typescript 中的装饰器向 class 添加和声明新属性(角度) - How to add and declare new property to class using decorator in typescript (angular) 如何使用Angular中的打字稿基于某些属性删除元素 - How to remove an element based on some property using typescript in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM