简体   繁体   English

Angular 2服务双向数据绑定

[英]Angular 2 Service Two-Way Data Binding

I have a salary.service and a player.component , if the salary variable gets updated in the service will the view in the player component be updated? 我有一个salary.service和一个player.component ,如果在服务中更新了工资变量,那么播放器组件中的视图会更新吗? Or is that not the case in Angular 2? 或者Angular 2中的情况不是这样吗?

When the page first loads I see the 50000 in the player.component view, so I know the two are working together. 当页面首次加载时,我在player.component视图中看到了50000,所以我知道这两个正在一起工作。 It's updating the value that's has me stumped. 它正在更新我难倒的价值。

salary.service salary.service

export class SalaryService {

    public salary = 50000; // starting value which gets subtracted from

    constructor() { }

    public setSalary = (value) => { this.salary = this.salary - value };

}

player.component player.component

export class PlayerComponent {

    constructor(private salaryService:SalaryService) {}

    public salary = this.salaryService.salary;

    public updateSalary = (value) => { this.salaryService.setSalary(value) };

}

EDIT 编辑

For anyone who wants to see how I resolved the issue, here's the Plunker: 对于任何想要了解我如何解决问题的人来说,这是Plunker:

http://plnkr.co/edit/aFRXHD3IAy0iFqHe5ard?p=preview http://plnkr.co/edit/aFRXHD3IAy0iFqHe5ard?p=preview

No, the way that you have it defined the public salary = this.salaryService.salary is copying out the value and not assigning the a reference to salary. 不,你有它的方式定义了public salary = this.salaryService.salary正在复制该值而不是分配对工资的引用。 They are distinct instances in memory and therefore one cannot expect the salary in the player component to be the same as the one in the service. 它们是内存中的不同实例,因此不能指望播放器组件中的工资与服务中的工资相同。

If you had a player with a salary and passed it to the service to operate on then the view would adjust correctly as it would be operating on the correct object. 如果你有一个有薪水的玩家并将其传递给服务进行操作,那么视图将正确调整,因为它将在正确的对象上操作。

That would look like: salary.service.ts 这看起来像: salary.service.ts

import {Injectable} from "@angular/core";

@Injectable()
export class SalaryService {
    constructor() { }

    public setSalary = (player, value) => {
      player.salary -= value;
    };

}

player.component.ts player.component.ts

import { Component } from "@angular/core";
import { SalaryService } from "./salary.service";

@Component({
  selector: 'player',
  template: `
  <div>{{player.salary}}</div>
  <button (click)="updateSalary(player, 50)" type="button">Update Salary</button>
  `
  providers: [SalaryService]
})
export class PlayerComponent {
    player = { id: 0, name: "Bob", salary: 50000};
    constructor(private salaryService:SalaryService) {

    }

    public updateSalary = (player, value) => {
      this.salaryService.setSalary(player, value);
    };
}

Finally, here is a plunker you can mess around with: http://plnkr.co/edit/oChP0joWuRXTAYFCsPbr?p=preview 最后,这里有一个你可以乱用的傻瓜: http ://plnkr.co/edit/oChP0joWuRXTAYFCsPbr?p = preview

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

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