如何从Angular 2中的指令调用服务

时间:2016-09-19 09:55:06

标签: jquery angularjs angular service directive

我无法通过此指令调用我的服务。我使用带有wizard.js的模板来制作向导模态表单。我不确定这是一个有角度的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();
          }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

删除function并将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();
          }
}