Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 从角度服务传递数据_Angular_Typescript_Ionic Framework_Ionic2_Ionic3 - Fatal编程技术网

Angular 从角度服务传递数据

Angular 从角度服务传递数据,angular,typescript,ionic-framework,ionic2,ionic3,Angular,Typescript,Ionic Framework,Ionic2,Ionic3,我在构造函数中有一个基本方法来计算一年中的当前月份并将值输出到HTML。然而,我似乎无法让它发挥作用 我是Angular 2和TypeScript的新手,所以这可能只是代表我犯的一个愚蠢的错误:) calendar.service.ts文件: import { Injectable } from '@angular/core'; import { AngularFireDatabase } from 'angularfire2/database'; @Injectable() export c

我在构造函数中有一个基本方法来计算一年中的当前月份并将值输出到HTML。然而,我似乎无法让它发挥作用

我是Angular 2和TypeScript的新手,所以这可能只是代表我犯的一个愚蠢的错误:)

calendar.service.ts文件:

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';

@Injectable()
export class GetMonthsService {
  private GetMonthsRef = this.fbDB.list('xxxx/xxxx/months');
  private GetDaysRef = this.fbDB.list('xxxx/xxxxx/months/Days');
  private currentMonth;

  constructor(private fbDB: AngularFireDatabase) {}

  getMonthsList() {
    return this.GetMonthsRef;
  }

  getDaysList() {
    return this.GetDaysRef;
  }

  getCurrentMonth() {
        let currentMonth;
        let d = new Date();
        return this.currentMonth = d.getMonth();
        }

}
home.html:

<ion-content padding>

    <ion-list-header>
      Current Month Page
    </ion-list-header>

          {{currentMonth}}

</ion-content>

当月页面
{{currentMonth}}

组件,您希望在其中使用服务并显示HTML

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {appService} from '../../providers/app.service';
import {GetMonthsService} from '../../providers/calendar.service';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController, public appService : appService, public getMonthsService : GetMonthsService) {
    getMonthsService.getCurrentMonth();
  }

}
HTML以显示来自服务的内容

<ion-header>
  <ion-navbar>
    <ion-title>Home</ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <h2>From calendar service {{getMonthsService.currentMonth}}</h2>

</ion-content>

您是否也有家用组件?请发布=)
getCurrentMonth
很奇怪,为什么不直接返回(新日期()).getMonth()。你知道getMonth return int 0..11吗?我们在
构造函数()中看不到任何方法。
?你能把组件的详细信息发布到你想使用这项服务的地方吗?非常感谢。很好的解释和工作示例。我的组件编码不好,无法正确调用服务。
import { Injectable } from '@angular/core';

@Injectable()
export class GetMonthsService {

  private currentMonth : any;

  constructor() {}


  getCurrentMonth() {
        let d = new Date();
        this.currentMonth = d.getMonth();
        return this.currentMonth;
        }

}