在角度,我如何从component.ts访问类方法? [英] In angular, how do I access a class method from component.ts?

查看:55
本文介绍了在角度,我如何从component.ts访问类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个有角度的项目,在这里我有一个按钮,希望它隐藏高图上除特定序列以外的所有序列.这可能很简单,但是我只是从事了一段时间的Web开发,而我在某些基础知识上却有点不足.我不是真正的网络开发人员,但同意承担一些工作,而不是让公司雇用知道自己在做什么的人.无论如何,合适的开发人员会为我设置项目,并向我展示如何做我想做的事情,但他目前正在休假,所以我变得有些困惑.

I am working on an angular project where I have a button where I want it to hide all but a particular series on a highchart. This is probably something simple, but I have only been doing web development for a little while and I'm a bit lacking on some of the basics. Im not really a web developer but agreed to take on some of the work instead of having the company hire someone who knew what they were doing. Anyway, a proper developer setup the project for me and showed me how to do what I wanted to do but he is currently on leave and I've become a little stuck.

因此,我正在使用高图显示一些数据.我有一个highcharts文件夹,其中包含一个称为highcharts-component.ts的可重用组件,如下所示:

So, I am using highcharts to display some data. I have a highcharts folder with a reusable component called highcharts-component.ts that looks like this:

import { Component, ElementRef, Input, OnDestroy, OnInit } from "@angular/core";
import { chart } from "highcharts";
import * as Highcharts from "highcharts";
import drilldown from "highcharts/modules/drilldown.src.js";
drilldown(Highcharts);
declare var require: any;
require("highcharts/highcharts-more")(Highcharts);
require("highcharts/modules/solid-gauge")(Highcharts);
require("highcharts/modules/heatmap")(Highcharts);
require("highcharts/modules/treemap")(Highcharts);
require("highcharts/modules/funnel")(Highcharts);

@Component({
  selector: "app-chart",
  styleUrls: ["./styles.scss"],
  templateUrl: "./template.html"
})
export class ChartComponent implements OnDestroy, OnInit {
  @Input()
  set options(value: Highcharts.Options) {
    this._options = value;
    this.chart = chart(this.elementRef.nativeElement, this._options);
  }

  private chart: Highcharts.ChartObject;
  private _options: Highcharts.Options;

  constructor(private elementRef: ElementRef) {}

  ngOnDestroy() {
    this.chart = null;
  }

  ngOnInit() {
    this.chart = chart(this.elementRef.nativeElement, this._options);
  }

  public showSingleSeries(seriesNumber) {
    for (let x = 0; x < this.chart.series.length; x++) {
      if (x == seriesNumber) {
        this.chart.series[seriesNumber].show();
      } else {
        this.chart.series[seriesNumber].hide();
      }
    }
  }
}

然后在我的名为card-fancy-example.ts的组件文件中,我有类似的内容:

Then in my component file called card-fancy-example.ts I have something like this:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule, OnInit, ViewChild, ElementRef, VERSION } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { Component } from '@angular/core';
import * as Highcharts from "highcharts";

/**
 * @title Card with multiple sections
 */
@Component({
  selector: 'card-fancy-example',
  templateUrl: 'card-fancy-example.html',
  styleUrls: ['card-fancy-example.css'],
})
export class CardFancyExample {

   myChart: Highcharts.Options = {
    // Created pie chart using Highchart
      chart: {
        type: 'pie'
      },
      title: {
        text: 'Contents using Pie chart'
      },
      subtitle: {
        text: '3D donut in Highcharts'
      },
      plotOptions: {
        pie: {
          innerSize: 100,
          depth: 45
        }
      }
  };
  constructor() {
  }
  ngOnInit() {
    this.loadChart();  
  }

  loadChart()
  {
    this.myChart = {
      ...this.myChart,
       series: [{
        name: 'Operating Systems',
        data: [
          {
            name: 'Windows',
            y: 88.19,
            drilldown: 'windows-versions'
          },
          ['MacOSX', 9.22],
          ['Linux', 1.58],
          ['Others', 1.01]
        ]
      }],
      drilldown: {
        series: [{
          name: 'Windows versions',
          id: 'windows-versions',
          data: [
            ['Win 7', 55.03],
            ['Win XP', 15.83],
            ['Win Vista', 3.59],
            ['Win 8', 7.56],
            ['Win 8.1', 6.18]
          ]
        }]
      }
    }
  }

  showSingleSeries(seriesNumber)
  { 
    // Where I want to call the showSingleSeries method in highcharts-component.ts
  }

}

我的html文件如下所示:

and my html file looks like this:

 <button (click)="showSingleSeries(0)">Remove All Series But Windows</button>
<button (click)="showSingleSeries(1)">Remove All Series But Linux</button>

    <app-chart [options]="myChart"></app-chart>

该图表可以正确加载,但是现在当我单击特定按钮时,我希望只能显示"Windows"或"MacOSX"系列.我在highcharts-component.ts中有一个方法可以做到这一点,称为showSingleSeries,如下所示:

The chart loads correctly but now when I click on a particular button I want to be able to only show either the "Windows" or the "MacOSX" series. I have a method to do this in highcharts-component.ts called showSingleSeries which looks like this:

public showSingleSeries(seriesNumber) {
    for (let x = 0; x < this.chart.series.length; x++) {
      if (x == seriesNumber) {
        this.chart.series[x].show();
      } else {
        this.chart.series[x].hide();
      }
    }
  }

问题是,我对angular的了解还不足以知道如何访问此showSingleSeries方法?我该怎么做呢?这是我在 stackblitz

The thing is, I don't know enough about angular to know how I can access this showSingleSeries method? How do I do this? Here is what I have in stackblitz

谢谢.

我在代码中编辑了两个错误,并更改了图表类型.我已经用解决方案更新了stackblitz.谢谢@Lys

I edited a couple of bugs in my code and changed the chart type. I have updated stackblitz with the solution. Thanks @Lys

推荐答案

编辑

您希望父级调用childs方法.您应该使用view child.

you want the parent to call the childs method. you should use view child.

< app-chart [options] ="myChart"#charts></app-chart>

@ViewChild('charts')图表; 然后将其命名为 this.charts.showSingleSeries(1);

这篇关于在角度,我如何从component.ts访问类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆