Angular 2/4:将服务层中的Promise函数的数组值分配给组件中的变量
创始人
2024-10-15 16:00:58
0

在Angular 2/4中,可以使用服务层中的Promise函数来获取数组值,并将其分配给组件中的变量。下面是一个示例解决方案:

在服务层中,创建一个返回Promise函数的方法,用于获取数组值。例如:

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

@Injectable()
export class MyService {
  getArrayValues(): Promise {
    return new Promise((resolve, reject) => {
      // 模拟异步获取数组值
      setTimeout(() => {
        resolve([1, 2, 3, 4, 5]); // 假设这是从服务获取的数组值
      }, 2000);
    });
  }
}

在组件中,使用服务层的方法来获取数组值,并将其分配给组件的变量。例如:

import { Component, OnInit } from '@angular/core';
import { MyService } from './my.service';

@Component({
  selector: 'app-my-component',
  templateUrl: './my.component.html',
  styleUrls: ['./my.component.css']
})
export class MyComponent implements OnInit {
  myArray: any[];

  constructor(private myService: MyService) { }

  ngOnInit() {
    this.myService.getArrayValues()
      .then((values) => {
        this.myArray = values;
      })
      .catch((error) => {
        console.error('Error:', error);
      });
  }
}

在组件的HTML模板中,可以使用*ngFor指令来循环遍历数组,并显示其值。例如:

  • {{ value }}

通过上述代码示例,你可以在Angular 2/4中将服务层中的Promise函数的数组值分配给组件中的变量。

相关内容

热门资讯

透视科普!fishpoker透... 透视科普!fishpoker透视底牌(透视)wepoker私人辅助器,教程指南书(证实有挂)-哔哩哔...
aapoker万能辅助器!wp... aapoker万能辅助器!wpk辅助哪里买(透视)技巧-本来揭露有挂该软件可以轻松地帮助玩家将aap...
透视了解!hhpoker辅助软... 透视了解!hhpoker辅助软件下载(透视)wepoker有插件吗,教程大纲(真是有挂)-哔哩哔哩1...
wejoker辅助器要钱玩吗!... wejoker辅助器要钱玩吗!线上德州的辅助器是什么(透视)软件-果然解迷有挂线上德州的辅助器是什么...
透视关于!wepoker透视有... 透视关于!wepoker透视有用吗(透视)wpk可以透视吗,教程窍门(有挂教学)-哔哩哔哩1、完成w...
aapoker真的假的!wep... aapoker真的假的!wepoker透视脚本下载(透视)挂-竟然详细存在有挂wepoker透视脚本...
透视关于!xpoker透视辅助... 透视关于!xpoker透视辅助(透视)约局吧德州透视,教程教程书(有挂规律)-哔哩哔哩约局吧德州透视...
hhpoker软件靠谱吗!we... hhpoker软件靠谱吗!wepoker私人局开挂视频(透视)教程-一直推荐是真的挂亲,关键说明,w...
透视关于!aapoker辅助工... 透视关于!aapoker辅助工具安全吗(透视)werplan辅助软件,教程绝活儿(确实有挂)-哔哩哔...
德普之星辅助工具如何设置!we... 德普之星辅助工具如何设置!wepoker轻量版书签(透视)工具-一直教你真的有挂德普之星辅助工具如何...