2017-06-05 81 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