The Zurich release has arrived! Interested in new features and functionalities? Click here for more

In Service Portal widgets, why do we use the c variable instead of $scope?

austinn1
Kilo Expert

What is the difference between using the c variable and using $scope? Does it make a difference?

3 REPLIES 3

DrewW
Mega Sage
Mega Sage

Interesting question, not sure what the real answer is but I always thought that it was because you may not need $scope in your widget.  But ultimately I believe if you enumerate $scope you will find "c" is part of it so it would be a shortcut to your server side data.

Justin77
Mega Guru

Someone on stackoverflow explained it very well I think: https://stackoverflow.com/a/14168699 (c = this)

Jesse20
Tera Contributor

From my understanding:

$scope is basically an object that contains everything about a widget/application/etc.. from it's variable definitions, method declarations, and function calls.

So, In the above client script think of it loosely like this: `$scope == c == this`. You can ignore the 'this' part here, but I included it just in case it helps with clarification. You typically see it in the first few lines of the client script,

api.controller=function() {
/* widget controller */
var c = this;


So, just like how you can use c.data.var to put data into the server, you can also use $scope.data.var to do the same. ServiceNow recommends to avoid using $scope unless you need to specifically target a different scope or ensure that the correct scope is being targeted when working with multiple scopes (broadcasting, cross widget communication, etc), because although the 'c' variable can access the $scope variable, the $scope variable is more broad and can also be different than the 'c' or 'this' that you're working within (the case with cross widget communication).

Hope that helps 😄