2017-06-05 80 views
0

我有一個按鈕綁定到一個函數,調用另一個函數從不同的組件打開模態窗體,作爲參數傳遞客戶名稱和圖標。原型和TypeScript Angular 2

工作正常,如果It's被稱爲像:

@Component({ 
    selector: 'app-customer', 
    templateUrl: './customer.component.html', 
    providers: [Items] 
}) 

export class Costumers { 
    @Input() customerNane 

    Constructor() {} 

    openDocs() { 
     Items.prototype.OpenDocs(this.customerNane, "fa-book")`; 
    } 
} 

但是,如果我有它的construtor並調用它像:

constructor(private items: Items) {} 

openDocs() { 
    this.items.OpenDocs(this.customerNmane, "fa-book")`; 
} 

參數wont't綁定。

非常感謝您的任何解釋!

更新.. items.component.ts

import { Component } from '@angular/core'; 
import { Http, Response, Headers, RequestOptions } from '@angular/http'; 
import { MainService } from '../services/main.service' 

var JQuery = require('jquery'); 


@Component({ 
    selector: 'app-items', 
    templateUrl: './items.component.html' 

}) 


export class Items { 
    public Title: string; 
    public faAny: string;  

    vTypeList = [ 

     { 
      vIdType: "", 
      vDsType: "" 
     } 
    ] 

    constructor(private http: Http, private mainService: MainService) { 

    this.GetItemsType(); 
    } 

    GetItemsType() { 

     this.http.get('/Items/GetItemsType') 
      .subscribe(data => { 
       var ObjTp = JSON.parse(data.json());     
       for (var i in ObjTp) { 
        if (ObjTp[i] != null) { 
         this.vTypeList.push(ObjTp[i]); 
        } 
       }    

      }); 

    } 


    OpenDocs(pTitle: string, pFaAny: string) { 

     this.Title = pTitle; 
     this.faAny = pFaAny; 
     JQuery('#docs').modal('show'); 

    } 

    private CloseDocs() { 

      JQuery('#docs').css("display", "none"); 
      JQuery('#docs').modal('hide'); 

     } 

    } 

和綁定的雙向綁定wont't

<h4 ng-model="Title" ><i class="fa {{faAny}}"></i> Add some list: {{Title}}</h4> 
+0

您提供'Item',不'Items'。但是,如果這些是你說的組件,你就不應該提供它們......提供服務。 「項目」是一項服務嗎? – dbandstra

+0

對不起,我拼錯了。更新問題。爲了回答你的問題,是和不是,因爲我正在使用「C#MVC模板」,現在我有一個'this.http.get('/ Items/GetItemsType')'。猜猜不..我沒有真正使用它作爲服務。 – Marisco

+0

你可以發佈什麼'項目?看起來像? – dbandstra

回答

0

參數。

使用箭頭功能。更改

openDocs() { 
    this.items.OpenDocs(this.customerNmane, "fa-book"); 
} 

openDocs =() => { 
    this.items.OpenDocs(this.customerNmane, "fa-book"); 
} 

爲什麼

原因箭頭功能可確保正確的this

更多

+0

沒有成功,我認爲可能是與生命的粒子左右..無論如何。 – Marisco