【问题标题】:how to make getter and setter in angular of input field model property?如何在输入字段模型属性的角度中制作getter和setter?
【发布时间】:2018-05-05 13:47:43
【问题描述】:

你能告诉我如何制作输入字段属性的getter和setter吗? 我试过这样

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
      private _keyValue: string;
      get KeyValue(): string{
        return this._keyValue;
    }

    set KeyValue(value: string){
      console.log('====')
        this._keyValue = value;
    }

  name = 'Angular 6';
}

当我在输入字段中输入时,我的console 没有打印出来,为什么? 这是我的代码 https://stackblitz.com/edit/angular-vnvv6b?file=src%2Fapp%2Fapp.component.ts

【问题讨论】:

  • 这可能对您有些价值:angular.io/guide/…
  • 你没有使用你创建的setter,你直接绑定到_keyValue
  • 我将如何使用?

标签: javascript angular


【解决方案1】:

你没有使用你创建的setter,你直接绑定到_keyValue

要绑定到你的setter,你必须使用

<input [(ngModel)]="KeyValue"/>

KeyValue 是你的 setter 的名字

【讨论】:

    猜你喜欢
    • 2011-02-23
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 2019-11-07
    • 2016-06-12
    相关资源
    最近更新 更多