Is there any way to access controller variable set in a Method in Javascript.
In the example above the value is set in constructor.
But when I set it in a method it is showing as NULL in Javascript
In controller:
public class abc {
public string varA { get;set; }
public void fn(){
varA='hello';
}
}
In VFPage:
function hello(){
var bool = '{!varA}';
alert(bool);
}
The alert statement in the VF Page(i.e bool) is displayed as null.
Can you please help how to access a variable set from a method instead of constructor?
Best Answer
Accessing controller properties uses the usual get & set syntax.
Set in a constructor and retrieved using the shorthand notation
or
Retrieved from the getNNN mechanism
or
Retrieved from a shorthand getter which calls a method in the controller
VF Page - JavaScript function - controller property reference will work with any of the above examples:
The rendered source for the page, which you can go look at in the browser, will look like this after the substitution for the controller variable has been made: