In Service Portal widgets, why do we use the c variable instead of $scope?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2021 08:07 AM
What is the difference between using the c variable and using $scope? Does it make a difference?
- Labels:
-
Service Portal Development
- 5,579 Views

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2021 08:34 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2021 10:34 AM
Someone on stackoverflow explained it very well I think: https://stackoverflow.com/a/14168699 (c = this)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2021 09:47 AM
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 😄