从child@Input setter-Ionic2/Angular2访问提供程序方法

从child@Input setter-Ionic2/Angular2访问提供程序方法,angular,typescript,ionic2,Angular,Typescript,Ionic2,我正在尝试从components@Input方法访问提供程序类。但是,调用@Input方法时,提供程序似乎不可用 下面是我的代码 #provider import { Injectable } from '@angular/core'; import 'rxjs/add/operator/map'; @Injectable() export class MyProvider { constructor() {} sampleMethod(){ return 'sample

我正在尝试从components
@Input
方法访问提供程序类。但是,调用
@Input
方法时,提供程序似乎不可用

下面是我的代码

#provider
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';


@Injectable()
export class MyProvider {

  constructor() {}

  sampleMethod(){
    return 'sample method';
  }
}

#component
import { Component, Input } from '@angular/core';
import {MyProvider} from '../../providers/my-provider/my-provider';
import { NavController} from 'ionic-angular';

@Component({
  selector: 'selected-options',
  templateUrl: 'build/components/selected-options/selected-options.html',
  inputs: ['node']
})
export class SelectedOptions {

  provider: MyProvider;

  @Input()
  set node(n: any){
     this.provider.sampleMethod();    
  }
}

#page (where I call the component) 
<selected-options [node]="node.Name"></selected-options>
#提供程序
从“@angular/core”导入{Injectable};
导入'rxjs/add/operator/map';
@可注射()
导出类MyProvider{
构造函数(){}
采样方法(){
返回“样本方法”;
}
}
#组成部分
从“@angular/core”导入{Component,Input};
从“../../providers/my provider/my provider”导入{MyProvider};
从'ionic angular'导入{NavController};
@组成部分({
选择器:“选定选项”,
templateUrl:'build/components/selected options/selected options.html',
输入:[“节点”]
})
导出类选择选项{
提供者:MyProvider;
@输入()
设置节点(n:任意){
this.provider.sampleMethod();
}
}
#页面(我在其中调用组件)
所以问题在于线路

this.provider.sampleMethod()

我得到的错误是
原始异常:TypeError:无法读取未定义的属性“sampleMethod”

所以我相信
provider:MyProvider@Input
方法时,不加载code>。但是,如果我在构造函数中使用它,它就可以正常工作。
如何访问
@Input
方法中的提供者方法?

要访问提供者,必须向应用程序介绍它们。关于app.ts上的ionic2,当您引导应用程序时,请添加您的提供商

import {YourProviderWithInjectableDecorator} from 'somepath';

    ionicBootstrap(MyApp, [YourProviderWithInjectableDecorator])
在你的课上

Component({
  selector: 'selected-options',
  templateUrl: 'build/components/selected-options/selected-options.html',
  inputs: ['node']
})
export class SelectedOptions {

  constructor(private provider: MyProvider){}

  @Input()
  set node(n: any){
     this.provider.sampleMethod();    
  }
}

希望它能有所帮助,这听起来可能会让人困惑,但您遇到的错误是,该提供程序没有作为参数包含在构造函数中(因此,您的构造函数没有创建
MyProvider
类的实例)

请看一看。就像您可以在那里使用se一样,即使我们在
@Input
setter拦截器中使用
myProvider
实例,构造函数已经创建了服务的实例,因此您可以毫无问题地调用
sampleMethod()

import { Component, Input } from "@angular/core";
import { MyProvider } from './my-provider.ts';

@Component({
  templateUrl:"child.html",
  selector: 'child-selector',
  inputs: ['node']
})
export class ChildPage {

  private result: string;

  @Input()
  set node(node: string){
    // Because the provider instance is injected on the constructor, we can
    // call the sampleMethod() here
    node = node + ' from ChildPage';
    this.result = this.myProvider.sampleMethod(node);
  }

  // Injects an instance of the MyProvider class
  constructor(private myProvider: MyProvider) {

  }
}

因此,如果您只是在构造函数中添加
private myProvider:myProvider
参数,那么您的代码应该可以正常工作:)

Hey@Sudakatux,谢谢您的回答,是的,我已经有了
ionicBootstrap(MyApp,[myProvider])@injectable stuff被注入构造函数()尝试创建构造函数方法Hey@Sebafereras,再次感谢您的帮助,以及关于这个问题的详细解释,解决了我的问题Np,这就是我们在这里的目的:)