我是Angular的新手,通常在Javascript js文件中,我可以执行put操作来更改引导开关的实际状态,如下所示:
$.ajax({
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token
},
url: "http://myapi/api/usuarios/activar",
data: JSON.stringify(data),
success: function(data) {
},
error: function(e) {
}
});
但现在我实现了Angular Bootstrap switch,我的component.html如下所示:
<mat-cell *matCellDef="let user">
<switch [status]="user.activo" [onText]="yes" [offText]="no" [onColor]="green" [offColor]="gray" [size]="normal" [disabled]="disabled"
(statusChange)="onFlagChange($event)"></switch>
</mat-cell>
正如你所看到的,它有(statusChange)="onFlagChange($event)
,它假设我如何在组件中创建事件,我知道我应该在服务中做类似这样的方法:
postSwitch() {
var switch = this.http
.put(this.rootUrl + "api/usuarios/activar", this.options)
.map((data: any) => data.json());
return switch;
}
那么我如何将它调用到component.ts中并在事件处理中执行它呢?问候
发布于 2018-06-07 03:31:42
onFlagChange($event)
指的是您在component.ts
文件中定义的函数,如下所示:
onFlagChange(event) {
this.yourServiceName.postSwitch().subscribe(() => {
//React to your HTTP request success
}, (err) => {
//React to your HTTP request error
});
}
您的服务模板:
@Injectable()
export class YourService {
constructor(private http: HttpClient) { }
//Your service functions
}
https://stackoverflow.com/questions/50728103
复制相似问题