简体   繁体   English

未从模型调用设置器/获取器

[英]Setter/getter not called from model

I have a problem where the two way binding is not working correctly and I assume that the getter and setter methods are the problem. 我有一个双向绑定不能正常工作的问题,我认为getter和setter方法是问题所在。

As you can see in the example am using the data property member.selectedConfig twice. 正如您在示例中看到的那样,我两次使用了data属性member.selectedConfig I aspect, that when the component is loaded the value of both of them should be "HelloWorld" but none of them is. 我看,加载组件时,它们两个的值都应该是"HelloWorld"但都不是。

When I start changing the value of the textbox manually also the displayed text of the other binding will change but the set function is not called. 当我开始手动更改文本框的值时,其他绑定的显示文本也将更改,但不会调用set函数。

The value of member.name is always displayed correctly. member.name的值始终正确显示。

When the getter and setter functions from the class BasicSystemMember is removed. 从类BasicSystemMember中删除getter和setter函数时。 The binding also works perfectly. 绑定也可以完美地工作。 But I need to have a setter function. 但是我需要有一个setter功能。

<li *ngFor="let member of activeSystemMembersList">
  <div class="">{{member.name}}</div>
  <div class="">{{member.selectedConfig}}</div>
  <input type="text" class="text-input" [(ngModel)]="member.selectedConfig" />
</li>
export class BasicSystemMember implements SystemMember{
  name: string;
  private _selectedConfig: string;

  constructor(){
    this._selectedConfig = "HelloWorld"
  }

  set selectedConfig(newName) {
    this._selectedConfig = newName
  };

  get selectedConfig() {
    return this._selectedConfig
  };
 }
export class SystemMembersComponent implements OnInit {
  activeSystemMembersList: SystemMember[];

  ngOnInit(){
    this.activeSystemMemberService.getActiveSystemMembers().subscribe(
      activeSystemMembersList => this.activeSystemMembersList = activeSystemMembersList)    
  }
}
export class ActiveSystemMemberService {    
  list: SystemMember[];    

  constructor() {
    this.list = []
  }
  getActiveSystemMembers(): Observable<SystemMember[]> {
    return of(this.list)
  } 
}

This issue is most often caused when creating the items, in your case, your members . 在创建项目时,通常是在您的情况下(在您的members引起此问题。 If you are creating the items as part of an Http request, something like this: 如果要作为Http请求的一部分创建项目,则如下所示:

getMembers(): Observable<BasicSystemMember[]> {
    return this.http.get<BasicSystemMember[]>(this.Url);

Then you are not actually creating BasicSystemMember objects in your array. 然后,您实际上并没有在数组中创建BasicSystemMember对象。 Rather, the Http request is creating elements that "look" like BasicSystemMember objects but they will only have the properties as provided by the Http request (not methods or getter/setters). 而是,Http请求正在创建看起来像BasicSystemMember对象的元素,但它们将仅具有Http请求提供的属性(而不是方法或getter / setter)。

For a single item, you could do something like this: 对于单个项目,您可以执行以下操作:

Object.assign(new BasicSystemMember(), member);

Something like this would work ... but could probably be much more efficiently coded (its late here LOL). 像这样的东西行得通...但是可能可以更有效地编码(在这里晚些时候,哈哈)。

getMembers(): Observable<BasicSystemMember[]> {
    return this.http.get<BasicSystemMember[]>(this.Url).pipe(
        map(data => {
           const newList: BasicSystemMember[] = [];
           data.forEach(item => 
                newList.push(Object.assign(new BasicSystemMember(), item)));
           return newList;
        })
     );
}

NOTE: I did not test or syntax check this. 注意:我没有测试或语法检查此。

This code maps the result from the Http response. 此代码映射Http响应的结果。 It creates an appropriately typed array. 它创建一个适当类型的数组。 Then foreach's through each element of the array using Object.assign to create an instance of the appropriate class and copy the item's data into it. 然后foreach使用Object.assign遍历数组的每个元素,以创建适当类的实例并将该项目的数据复制到其中。 Then it pushes it onto the array. 然后将其推入阵列。

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

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