简体   繁体   English

如何更改service.ts angular中的变量值?

[英]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.

相关问题 如何在angular service.ts中传递fix变量? - How to pass fix variable in angular service.ts? 如何在另一个函数中更改变量后在另一个函数中获取新值,在 Angular ...service.ts - How to get the new value of a variable in another function after it was changed inside a different function, in Angular ...service.ts 如何将 component.ts 中的变量发送到 service.ts 进行身份验证 Angular - How to send variable in component.ts to service.ts for authentication Angular 如何绑定选择/选项值以在service.ts中而不是component.ts中获得请求功能(角度8) - How to bind selected/option value to get request function in service.ts and not component.ts (Angular 8) 为什么在 service.ts 中刷新页面时我的变量值会发生变化? - Why my variable's value change when page refresh in service.ts? 如何在我的 html - IONIC 2 上显示来自 service.ts 的变量值 - How to show value of variable from a service.ts on my html - IONIC 2 Angular 8 如何在service.ts文件中使用动画过渡和触发器 - Angular 8 how to use Animations transitions and triggers in a service.ts file Angular2,如何在service.ts中使用JSON数据? - Angular2, How to use JSON data in service.ts? 从角度为6的service.ts返回undefined - return undefined from service.ts in angular 6 如何在发射成功时处理从 service.ts 到 component.ts 的 socket.io 确认 Angular 8 - How to handle socket.io acknowledgement from service.ts to component.ts on emit success Angular 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM