[英]How to change variable value in service.ts angular?
I'm pretty new as a programmer and with the angular framework too, so the question can be very obvious. 作为一个程序员,我也有了角度框架,这对我来说还是个新手,所以问题很明显。
I have a service.ts file that runs CRUD with firebase and inside Firebase I am performing a filter that depends on the value of the variable " this.machine ". 我有一个使用firebase运行CRUD的service.ts文件,在Firebase内部,我正在执行一个过滤器,该过滤器取决于变量“ this.machine ”的值。 Depending on the machine name a filter is performed and shows data only from this machine.
根据计算机名称,将执行过滤器并仅显示来自该计算机的数据。
This is a part of the code: 这是代码的一部分:
@Injectable()
export class CrudCadastroService {
public machineList: Machine[];
cadastroList: AngularFireList<any>;
selectedCadastro: CadastroModal = new CadastroModal();
machine: string;
constructor(
private firebase :AngularFireDatabase
) { }
getData(){
this.cadastroList = this.firebase.list('cadastros', ref => ref.orderByChild('machine').equalTo(**this.machine**));
return this.cadastroList;
}
I have another service.ts that push list from Firebase database and I'm using in component.ts. 我还有另一个从Firebase数据库推送列表的service.ts,我正在component.ts中使用。
In this file componete.ts I get the value of this variable. 在这个文件componete.ts中,我得到了这个变量的值。
changeShape(shape) {
//Get name machine value
//I want to use this variable **machine** into service.ts
let machine= shape.value;
console.log(machine) //name of machine
}
This is my html 这是我的HTML
<div class="form-group">
<select name="machine" #machine="ngModel [(ngModel)]="cadastroService.selectedCadastro.machine"
(change)="changeShape($event.target)" >
<option *ngFor="let machine of machineList"
>{{machine.name}}</option>
</select>
</div>
My question is, can I change the value of this.machine in the service.ts file? 我的问题是,我可以在service.ts文件中更改this.machine的值吗? Is there any different way of doing this logic?
有没有其他方法可以执行此逻辑?
您可以在service.ts和component.ts中创建setter方法,然后再调用以获取该计算机的列表,然后将setter方法调用为计算机值。
在打字稿中,默认情况下访问器是公共的,因此,如果将CrudCadastroService作为“ crudCastroService”注入到组件中,则只需更改其在component.ts中的值即可。
this.crudCastroService.machine = 'newName';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.