【发布时间】:2016-08-17 01:33:27
【问题描述】:
我正在尝试从我的组件@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>
所以问题在于行
this.provider.sampleMethod();
我得到的错误是ORIGINAL EXCEPTION: TypeError: Cannot read property 'sampleMethod' of undefined。
所以我相信在调用@Input 方法时不会加载provider: MyProvider;。但是如果我在构造函数中使用它,这很好。
如何访问 @Input 方法中的提供程序方法?
【问题讨论】:
标签: angular typescript ionic2