-1

instead of:

 sourceFormControl.setErrors({ "scoresGreaterThan": true });

I need it dynamic:

 let errorKey = "scoresGreaterThan";
 sourceFormControl.setErrors({ errorKey: true });

but that did not work :-/

I want the content of the errorKey to be the key of the object literal passed to the setErrors method.

How can I do that with TypeScript?

Pascal
  • 12,265
  • 25
  • 103
  • 195

2 Answers2

2

Drop the key inside square brackets.

sourceFormControl.setErrors({ [errorKey]: true });
kind user
  • 40,029
  • 7
  • 67
  • 77
  • 1
    just found it too: http://stackoverflow.com/questions/19837916/creating-object-with-dynamic-keys and it worked, thx! – Pascal Apr 28 '17 at 21:08
0

One way is to create an object and then assign a value to that key.

 let errorKey = "scoresGreaterThan";
 let obj = {};
 obj[errorKey] = true;
 sourceFormControl.setErrors(obj);
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60