在角度5中使用此变量

时间:2018-07-18 07:36:09

标签: angular

我想在this.中使用一个变量。我已经做过,但是那是一段时间以前的,我不记得该怎么做。通过Google或此处进行搜索没有给我带来任何帮助。

这是我的代码:

fieldFocused: String; (gets filled by another function)

const helper = this.fieldFocused;

this.[helper]Model = this.[helper]Model + this.keyboardKeyPressed;

因此,我想将我的this.[helper]Model“转换”为this.usernameModel,然后获取'john'的值,我该怎么做?

[tl; dr]

我实际上正在处理具有翻转屏幕的树莓派3,因此像大智能手机一样可用。屏幕键盘无法满足我的需求,因此我只能自己创建一个。就像我说的那样,有三项输入:usernamepasswordipaddress。现在,当用户单击这些按钮之一时,我将this.fieldFocused设置为用户单击的字段,然后我想根据最后关注的字段来编辑模型。

  1. 用户点击输入用户名
  2. this.focusedInput设置为用户名
  3. 他按下键盘上的按键“ a”以调用函数
  4. 取决于所关注的领域,我必须编辑模型,这就是为什么我需要在另一个this.focusedInput-> this.
  5. 中使用this.[focusedInput]的原因

1 个答案:

答案 0 :(得分:2)

您要查找的是属性访问器的方括号表示法:this[helper + 'Model']

更新的代码:

fieldFocused: String; // (gets filled by another function)

const helper = this.fieldFocused;

this[helper + 'Model'] = this[helper + 'Model'] + this.keyboardKeyPressed;

Further Reading