我在Angular 6中有一个服务,我正在尝试更改记录,但它说我没有被授权.
现在我有这个:
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
update(id, title, content) {
const updateData = { id: id, title: title, content: content };
return this.http.put(`http://myurl/${id}`, updateData, httpOptions);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
如何向httpOptions添加基本授权或直接将其添加到更新方法?
我有一个preject包含一个服务(获取数据)和一个组件(显示它).
我想在app.component.ts上将代码保持在最低限度.
在我的服务中:
getPosts() {
return this.http.get('https://jsonplaceholder.typicode.com/posts', httpOptions).subscribe(
result => {
return result;
this.theData = result;
},
error => {
return error;
}
);
}
Run Code Online (Sandbox Code Playgroud)
在我的app.component.ts中:
getPostsFromService() {
this.myService.getPosts();
}
Run Code Online (Sandbox Code Playgroud)
但是,当然,我需要得到结果并将其传递给我的组件,但这样的事情是行不通的:
myData;
...
getPostsFromService() {
this.myData = this.myService.getPosts();
}
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是,我该怎么做或者是否真的建议在我的组件上调用subscribe而不是在服务中?
我正在使用@ViewChild是Angular.
在我的app.component.html上,我有:
<div #somename>Mark<span class="somethingelse">Hello something else</span></div>
Run Code Online (Sandbox Code Playgroud)
在我的app.component.ts上
@ViewChild('somename') SomeName;
constructor() {}
getInputValue() {
alert(this.SomeName.nativeElement('span').getElementByClassName('somethingelse').innerHTML);
}
Run Code Online (Sandbox Code Playgroud)
这不会返回任何东西.
我的问题是如何定位跨度的类并获得它的价值?