Set variable from function
This may be an easy question/answer, but I can't seem to wrap my head around it:
I am validating fields with a function using AbstractControl:
errorVar: boolean = false
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
let errorMessageText: "test"
return 'errorText': errorMessageText;
return null;
Besides the errorText
I want the function to also set the variable errorVar
to true
and to false
if the function returns null.
angular typescript
add a comment |
This may be an easy question/answer, but I can't seem to wrap my head around it:
I am validating fields with a function using AbstractControl:
errorVar: boolean = false
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
let errorMessageText: "test"
return 'errorText': errorMessageText;
return null;
Besides the errorText
I want the function to also set the variable errorVar
to true
and to false
if the function returns null.
angular typescript
And what is your problem?
– Daniel Hilgarth
Nov 14 '18 at 9:15
If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable
– PhilipK
Nov 14 '18 at 10:47
add a comment |
This may be an easy question/answer, but I can't seem to wrap my head around it:
I am validating fields with a function using AbstractControl:
errorVar: boolean = false
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
let errorMessageText: "test"
return 'errorText': errorMessageText;
return null;
Besides the errorText
I want the function to also set the variable errorVar
to true
and to false
if the function returns null.
angular typescript
This may be an easy question/answer, but I can't seem to wrap my head around it:
I am validating fields with a function using AbstractControl:
errorVar: boolean = false
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
let errorMessageText: "test"
return 'errorText': errorMessageText;
return null;
Besides the errorText
I want the function to also set the variable errorVar
to true
and to false
if the function returns null.
angular typescript
angular typescript
edited Nov 14 '18 at 10:48
PhilipK
asked Nov 14 '18 at 9:14
PhilipKPhilipK
238
238
And what is your problem?
– Daniel Hilgarth
Nov 14 '18 at 9:15
If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable
– PhilipK
Nov 14 '18 at 10:47
add a comment |
And what is your problem?
– Daniel Hilgarth
Nov 14 '18 at 9:15
If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable
– PhilipK
Nov 14 '18 at 10:47
And what is your problem?
– Daniel Hilgarth
Nov 14 '18 at 9:15
And what is your problem?
– Daniel Hilgarth
Nov 14 '18 at 9:15
If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable
– PhilipK
Nov 14 '18 at 10:47
If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable
– PhilipK
Nov 14 '18 at 10:47
add a comment |
2 Answers
2
active
oldest
votes
You can do something like this:
errorVar: boolean = false
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if (test)
this.errorVar = true;
let errorMessageText:
return 'errorText': errorMessageText ;
this.errorVar = false;
return null;
Nope, tried that first of course. It comes back "undefined is not an object"
– PhilipK
Nov 14 '18 at 10:39
IserrorVar
defined within the context of aclass
?
– Hayden Hall
Nov 14 '18 at 10:48
yes, it is - it's a reactive form and the variable definition and function is inside the export class
– PhilipK
Nov 14 '18 at 10:52
add a comment |
I think there are several problems
- your
errorVar
should be declared usingvar
orlet
so you can set it in the function via a closure - your
errorMessageText
variable must be set using=
and not:
like this:
var errorVar: boolean = false;
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
errorVar = true;
let errorMessageText: string ="test";
return 'errorText': errorMessageText;
errorVar = false;
return null;
Thanks for your answer, but that's not why it's not working. TheerrorVar: boolean = false
was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.
– PhilipK
Nov 15 '18 at 8:08
Well, I tested my code on TypeScript playground.let errorMessageText: "test"
did not work (errorMessageText
was undefined), butlet errorMessageText= "test"
did.
– XouDo
Nov 15 '18 at 9:12
Concerning the "pointer" thing, what you can do is pass an objecterrorMessageText:""
as parameter, and modify its property inside the function.
– XouDo
Nov 15 '18 at 9:22
Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am settingerrorMessageText
as type string.... but all that is beside the point, because the function properly returnserrorMessageText
, what I need is that the function returns a SECOND variable!
– PhilipK
Nov 17 '18 at 10:28
Ok, this was a typo error. Could it be that you have a problem ofthis
binding that prevents you from settingserrorVar
(if it's declared in a class)? In that case, you could bind explicitly by passingmyFunction.bind(myInstance)
instead ofmyFunction
. Anyway, good luck for your problem.
– XouDo
Nov 18 '18 at 6:36
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296586%2fset-variable-from-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do something like this:
errorVar: boolean = false
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if (test)
this.errorVar = true;
let errorMessageText:
return 'errorText': errorMessageText ;
this.errorVar = false;
return null;
Nope, tried that first of course. It comes back "undefined is not an object"
– PhilipK
Nov 14 '18 at 10:39
IserrorVar
defined within the context of aclass
?
– Hayden Hall
Nov 14 '18 at 10:48
yes, it is - it's a reactive form and the variable definition and function is inside the export class
– PhilipK
Nov 14 '18 at 10:52
add a comment |
You can do something like this:
errorVar: boolean = false
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if (test)
this.errorVar = true;
let errorMessageText:
return 'errorText': errorMessageText ;
this.errorVar = false;
return null;
Nope, tried that first of course. It comes back "undefined is not an object"
– PhilipK
Nov 14 '18 at 10:39
IserrorVar
defined within the context of aclass
?
– Hayden Hall
Nov 14 '18 at 10:48
yes, it is - it's a reactive form and the variable definition and function is inside the export class
– PhilipK
Nov 14 '18 at 10:52
add a comment |
You can do something like this:
errorVar: boolean = false
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if (test)
this.errorVar = true;
let errorMessageText:
return 'errorText': errorMessageText ;
this.errorVar = false;
return null;
You can do something like this:
errorVar: boolean = false
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if (test)
this.errorVar = true;
let errorMessageText:
return 'errorText': errorMessageText ;
this.errorVar = false;
return null;
edited Nov 14 '18 at 9:25
Oram
810518
810518
answered Nov 14 '18 at 9:19
Hayden HallHayden Hall
24136
24136
Nope, tried that first of course. It comes back "undefined is not an object"
– PhilipK
Nov 14 '18 at 10:39
IserrorVar
defined within the context of aclass
?
– Hayden Hall
Nov 14 '18 at 10:48
yes, it is - it's a reactive form and the variable definition and function is inside the export class
– PhilipK
Nov 14 '18 at 10:52
add a comment |
Nope, tried that first of course. It comes back "undefined is not an object"
– PhilipK
Nov 14 '18 at 10:39
IserrorVar
defined within the context of aclass
?
– Hayden Hall
Nov 14 '18 at 10:48
yes, it is - it's a reactive form and the variable definition and function is inside the export class
– PhilipK
Nov 14 '18 at 10:52
Nope, tried that first of course. It comes back "undefined is not an object"
– PhilipK
Nov 14 '18 at 10:39
Nope, tried that first of course. It comes back "undefined is not an object"
– PhilipK
Nov 14 '18 at 10:39
Is
errorVar
defined within the context of a class
?– Hayden Hall
Nov 14 '18 at 10:48
Is
errorVar
defined within the context of a class
?– Hayden Hall
Nov 14 '18 at 10:48
yes, it is - it's a reactive form and the variable definition and function is inside the export class
– PhilipK
Nov 14 '18 at 10:52
yes, it is - it's a reactive form and the variable definition and function is inside the export class
– PhilipK
Nov 14 '18 at 10:52
add a comment |
I think there are several problems
- your
errorVar
should be declared usingvar
orlet
so you can set it in the function via a closure - your
errorMessageText
variable must be set using=
and not:
like this:
var errorVar: boolean = false;
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
errorVar = true;
let errorMessageText: string ="test";
return 'errorText': errorMessageText;
errorVar = false;
return null;
Thanks for your answer, but that's not why it's not working. TheerrorVar: boolean = false
was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.
– PhilipK
Nov 15 '18 at 8:08
Well, I tested my code on TypeScript playground.let errorMessageText: "test"
did not work (errorMessageText
was undefined), butlet errorMessageText= "test"
did.
– XouDo
Nov 15 '18 at 9:12
Concerning the "pointer" thing, what you can do is pass an objecterrorMessageText:""
as parameter, and modify its property inside the function.
– XouDo
Nov 15 '18 at 9:22
Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am settingerrorMessageText
as type string.... but all that is beside the point, because the function properly returnserrorMessageText
, what I need is that the function returns a SECOND variable!
– PhilipK
Nov 17 '18 at 10:28
Ok, this was a typo error. Could it be that you have a problem ofthis
binding that prevents you from settingserrorVar
(if it's declared in a class)? In that case, you could bind explicitly by passingmyFunction.bind(myInstance)
instead ofmyFunction
. Anyway, good luck for your problem.
– XouDo
Nov 18 '18 at 6:36
add a comment |
I think there are several problems
- your
errorVar
should be declared usingvar
orlet
so you can set it in the function via a closure - your
errorMessageText
variable must be set using=
and not:
like this:
var errorVar: boolean = false;
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
errorVar = true;
let errorMessageText: string ="test";
return 'errorText': errorMessageText;
errorVar = false;
return null;
Thanks for your answer, but that's not why it's not working. TheerrorVar: boolean = false
was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.
– PhilipK
Nov 15 '18 at 8:08
Well, I tested my code on TypeScript playground.let errorMessageText: "test"
did not work (errorMessageText
was undefined), butlet errorMessageText= "test"
did.
– XouDo
Nov 15 '18 at 9:12
Concerning the "pointer" thing, what you can do is pass an objecterrorMessageText:""
as parameter, and modify its property inside the function.
– XouDo
Nov 15 '18 at 9:22
Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am settingerrorMessageText
as type string.... but all that is beside the point, because the function properly returnserrorMessageText
, what I need is that the function returns a SECOND variable!
– PhilipK
Nov 17 '18 at 10:28
Ok, this was a typo error. Could it be that you have a problem ofthis
binding that prevents you from settingserrorVar
(if it's declared in a class)? In that case, you could bind explicitly by passingmyFunction.bind(myInstance)
instead ofmyFunction
. Anyway, good luck for your problem.
– XouDo
Nov 18 '18 at 6:36
add a comment |
I think there are several problems
- your
errorVar
should be declared usingvar
orlet
so you can set it in the function via a closure - your
errorMessageText
variable must be set using=
and not:
like this:
var errorVar: boolean = false;
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
errorVar = true;
let errorMessageText: string ="test";
return 'errorText': errorMessageText;
errorVar = false;
return null;
I think there are several problems
- your
errorVar
should be declared usingvar
orlet
so you can set it in the function via a closure - your
errorMessageText
variable must be set using=
and not:
like this:
var errorVar: boolean = false;
function(c: AbstractControl): [key: string]: string | null
// validation if 'test' is true or not goes here
if(test)
errorVar = true;
let errorMessageText: string ="test";
return 'errorText': errorMessageText;
errorVar = false;
return null;
answered Nov 14 '18 at 13:12
XouDoXouDo
6916
6916
Thanks for your answer, but that's not why it's not working. TheerrorVar: boolean = false
was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.
– PhilipK
Nov 15 '18 at 8:08
Well, I tested my code on TypeScript playground.let errorMessageText: "test"
did not work (errorMessageText
was undefined), butlet errorMessageText= "test"
did.
– XouDo
Nov 15 '18 at 9:12
Concerning the "pointer" thing, what you can do is pass an objecterrorMessageText:""
as parameter, and modify its property inside the function.
– XouDo
Nov 15 '18 at 9:22
Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am settingerrorMessageText
as type string.... but all that is beside the point, because the function properly returnserrorMessageText
, what I need is that the function returns a SECOND variable!
– PhilipK
Nov 17 '18 at 10:28
Ok, this was a typo error. Could it be that you have a problem ofthis
binding that prevents you from settingserrorVar
(if it's declared in a class)? In that case, you could bind explicitly by passingmyFunction.bind(myInstance)
instead ofmyFunction
. Anyway, good luck for your problem.
– XouDo
Nov 18 '18 at 6:36
add a comment |
Thanks for your answer, but that's not why it's not working. TheerrorVar: boolean = false
was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.
– PhilipK
Nov 15 '18 at 8:08
Well, I tested my code on TypeScript playground.let errorMessageText: "test"
did not work (errorMessageText
was undefined), butlet errorMessageText= "test"
did.
– XouDo
Nov 15 '18 at 9:12
Concerning the "pointer" thing, what you can do is pass an objecterrorMessageText:""
as parameter, and modify its property inside the function.
– XouDo
Nov 15 '18 at 9:22
Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am settingerrorMessageText
as type string.... but all that is beside the point, because the function properly returnserrorMessageText
, what I need is that the function returns a SECOND variable!
– PhilipK
Nov 17 '18 at 10:28
Ok, this was a typo error. Could it be that you have a problem ofthis
binding that prevents you from settingserrorVar
(if it's declared in a class)? In that case, you could bind explicitly by passingmyFunction.bind(myInstance)
instead ofmyFunction
. Anyway, good luck for your problem.
– XouDo
Nov 18 '18 at 6:36
Thanks for your answer, but that's not why it's not working. The
errorVar: boolean = false
was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.– PhilipK
Nov 15 '18 at 8:08
Thanks for your answer, but that's not why it's not working. The
errorVar: boolean = false
was meant as a pointer that I have declared the variable earlier in the class; the function is a representation what I want to achieve, not a functioning piece of code. Not that I can explain it, but rest assured, I have about 20 other declarations that work just fine this way. ...and to your second point, TypeScript let's you define the variable type by using a colon after the name.– PhilipK
Nov 15 '18 at 8:08
Well, I tested my code on TypeScript playground.
let errorMessageText: "test"
did not work (errorMessageText
was undefined), but let errorMessageText= "test"
did.– XouDo
Nov 15 '18 at 9:12
Well, I tested my code on TypeScript playground.
let errorMessageText: "test"
did not work (errorMessageText
was undefined), but let errorMessageText= "test"
did.– XouDo
Nov 15 '18 at 9:12
Concerning the "pointer" thing, what you can do is pass an object
errorMessageText:""
as parameter, and modify its property inside the function.– XouDo
Nov 15 '18 at 9:22
Concerning the "pointer" thing, what you can do is pass an object
errorMessageText:""
as parameter, and modify its property inside the function.– XouDo
Nov 15 '18 at 9:22
Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am setting
errorMessageText
as type string.... but all that is beside the point, because the function properly returns errorMessageText
, what I need is that the function returns a SECOND variable!– PhilipK
Nov 17 '18 at 10:28
Of course it didn't, because as I said before, you define the type of the variable with the colon ":" and write it with the equal sign "=". So in this example I am setting
errorMessageText
as type string.... but all that is beside the point, because the function properly returns errorMessageText
, what I need is that the function returns a SECOND variable!– PhilipK
Nov 17 '18 at 10:28
Ok, this was a typo error. Could it be that you have a problem of
this
binding that prevents you from settings errorVar
(if it's declared in a class)? In that case, you could bind explicitly by passing myFunction.bind(myInstance)
instead of myFunction
. Anyway, good luck for your problem.– XouDo
Nov 18 '18 at 6:36
Ok, this was a typo error. Could it be that you have a problem of
this
binding that prevents you from settings errorVar
(if it's declared in a class)? In that case, you could bind explicitly by passing myFunction.bind(myInstance)
instead of myFunction
. Anyway, good luck for your problem.– XouDo
Nov 18 '18 at 6:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296586%2fset-variable-from-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
And what is your problem?
– Daniel Hilgarth
Nov 14 '18 at 9:15
If I try to set the variable inside the function it comes back as "undefined is not an object. So I'm fairly new to this and can't figure out why. So my idea was to have the function return the variable
– PhilipK
Nov 14 '18 at 10:47