简体   繁体   English

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

[英]How can i call a service from a directive in Angular 2

I can't call my service from this directive. 我无法从该指令调用服务。 I use a template with with wizard.js to make a wizard modal form. 我将模板与wizard.js一起使用以制作向导模态表单。 I'm not sure if it's a angular 2 problem btw. 我不确定这是否是角度2问题。

Here is my directive : 这是我的指令:

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();
          }
}

Any ideas ? 有任何想法吗 ?

Remove function and replace function () by () => 删除function并用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();
          }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM