如何从Angular 2的指令中调用服务
问题描述:
我无法通过此指令调用我的服务。我使用带有wizard.js的模板来制作向导模式窗体。我不确定这是否是一个角度2问题。如何从Angular 2的指令中调用服务
这里是我的指令:
import {Directive,Input, EventEmitter, ElementRef, Output} from '@angular/core';
import {Injectable,Inject} from '@angular/core';
import {Service} from '../../../service/service';
@Directive ({
selector: '[bootstrap-application-wizard]',
providers:[DashboardService]
})
export class BootstrapApplicationWizard {
$el: any;
constructor(
private _service:Service){}
function render(){
wizard.on('submit', function(wizard): void {
this.submit = {'data1': 'John','data2': 'Doe'};
this._service.postData('data',this.submit).subscribe(x=>{
console.log('ok');
});
}
ngOnInit(){
this.render();
}
}
任何想法?
答
删除function
和() =>
@Directive ({
selector: '[bootstrap-application-wizard]',
providers:[DashboardService]
})
export class BootstrapApplicationWizard {
$el: any;
constructor(private _service:Service){}
render(){
wizard.on('submit', (wizard): void => {
this.submit = {'data1': 'John','data2': 'Doe'};
this._service.postData('data',this.submit).subscribe(x=>{
console.log('ok');
});
}
ngOnInit(){
this.render();
}
}
更换
function()