简体   繁体   English

使用 Angular 和 PUT 更新整个组件

[英]Update whole Component with Angular and PUT

I want to Update a Angular Component with put to the Server.我想更新一个 Angular 组件并放入服务器。

for example i have following html element.例如,我有以下 html 元素。

<div class="input-group mb-3">
                        <span class="input-group-text" id="basic-addon1">Vendor</span>
                        <input type="text" class="form-control" placeholder={{this.data[1]}}
                            formControlName="vendorForm">
                    </div>

I emit the data to the father component with @Output. `this.event.emit(this.inputFormAllgInfo);`

Also I implemented the function to pass the data to the server我还实现了 function 将数据传递到服务器

public updateComponentDetails(identify: string, value: componentAttributes) {
    return this.client
      .put(this.appConf.apiBaseUrl + 'components/' + this.componentID, value)
      .subscribe();
  }

How can I update all data at once?如何一次更新所有数据?

A http client subscribe method has a next and a error response like: http 客户端订阅方法有一个 next 和一个错误响应,例如:

this.client.put("URL", data).subscribe({next: (data) => { // Your data}, err: (err) => { // The error})

Part two (Don't need a client. It's only a example for change detection) HTML Part第二部分(不需要客户端。它只是一个变化检测的例子) HTML 部分

<div>
  <button (click)="onClick()">CLICK ME</button>
  <div *ngIf="isVisible">I'm the visible part!</div>
  <div *ngIf="!isVisible" style="color: red;">Hey! What I doing here?</div>
</div>

Code Behind代码隐藏

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  isVisible: boolean = false;

  onClick() {
    this.isVisible = !this.isVisible;
  }
}

So you can change the isVisible variable with the service like:因此,您可以使用以下服务更改 isVisible 变量:

Service:服务:

@Injectable({
  providedIn: 'root',
})
export class MyService {
  public getSomeData() {
    return this.client.put("URL", data);
  }
}

Back to Code Behind:回到代码背后:

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  isVisible: boolean = false;

  constructor(private myService: MyService) { // Don't forget to import it
  }

  onClick() {
    this.myService.getSomeData().subscribe(
      { next: (data) => { this.isVisible = data; }, 
        err: (err) => { // The error})
    // this.isVisible = !this.isVisible;
  }
}

So this all is basic knowledge.所以这都是基础知识。

Greetings, Florian问候,弗洛里安

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM