小编Mar*_*018的帖子

Angular 6 - httpClient在httpOptions中传递基本身份验证

我在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添加基本授权或直接将其添加到更新方法?

typescript angular angular6

14
推荐指数
3
解决办法
3万
查看次数

Angular 6 - HttpClient在服务中保留订阅,但将结果传递给组件

我有一个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而不是在服务中?

typescript angular angular-httpclient angular6

7
推荐指数
1
解决办法
2万
查看次数

Angular 6 - @ViewChild nativeElement span class的访问值

我正在使用@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)

这不会返回任何东西.

我的问题是如何定位跨度的类并获得它的价值?

typescript angular angular6

1
推荐指数
1
解决办法
1万
查看次数