service portals
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
hello everyone,
When to use c. and $scope in service portals scripting?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
In ServiceNow Service Portal, c and $scope serve different purposes and are used in different scenarios.
Use c (Controller)
c is the controller object (var c = this;) and is the recommended way to expose data and functions to your widget's HTML.
Use it for:
Binding data to the UI ({{c.variable}})
Handling button clicks and other user interactions
Calling server scripts using c.server.get(), c.server.update(), or c.server.refresh()
Storing widget state and client-side logic
Example:
api.controller = function() {
var c = this;
c.message = "Hello Service Portal";
c.sayHello = function() {
alert(c.message);
};
};Use $scope
$scope is part of AngularJS and is mainly used for Angular-specific features rather than general widget development.
Common use cases include:
Listening for events using $scope.$on()
Broadcasting or emitting events using $scope.$broadcast() and $scope.$emit()
Watching variables with $scope.$watch()
Integrating with Angular directives or legacy Angular code that depends on $scope
Example:
api.controller = function($scope) {
var c = this;
$scope.$on('sp.page.loaded', function() {
console.log('Page loaded');
});
};