2017-10-10 86 views
0

我是Angular2的新手,对我很袒护。我有一个DataService应该获得文本框的值,并将其传递到我的组件。我想知道是否有一种方法来访问我的index.html文件中的文本框值更改时的服务对象?这是我到目前为止已经试过:Angular2在index.html中访问服务对象

的DataService:

export interface Info{ 
    itemNo:string; 
} 

@Injectable() 
export class DataService { 

item:Info={itemNo:"Test"}; 

change(newItem:string){ 
    this.item.itemNo=newItem; 
} 
constructor(private http: Http) { } 
} 

我想从业务接入改变方法:

onSelect: function (request, response) { 
    console.log(urlApi); 
      $.ajax({ 
       type: "GET", 
       url: urlApi, 
       dataType: 'json', 
       success: function(response) { 
        this.item.change(response.items[0].item); //This is what I am trying to do 
       }, 
       error: function() { alert('Failed!'); }, 
      }); 
     } 

有人能指出我怎么能做到这一点?

回答

0

我认为你的问题是与“这个”范围。如果您可以使用ES6,则可以使用新的arrow functions访问此项目。它可能和...一样简单。

... 
 
success: (response) =>{ 
 
    this.item.change(response.items[0].item); //This is what I am trying to do 
 
} 
 
...

虽然说了这么多,你可能采用了棱角分明的HTTP client而不是jQuery的

会更好
相关问题