$scope is preventing my server script to run in server script of service portal widget

jordimsant
Tera Guru

I am trying to develop a simple button widget, just for learning purposes. My idea was to take the default link-button and make a "function button" such that it executes a function when it is clicked. The code is as follows:

HTML:

<button class="btn btn-{{options.color}} m-b" ng-click="funcio()">{{data.buttonMsg}}</button>

Server Script:

data.buttonMsg = gs.getMessage(options.button_text || "Click Here");
gs.log('Holaaa', 'WIDGET');

Client Script:

api.controller=function() {
  /* widget controller */
  var c = this;
  $scope.funcio = function() {
	var url = 'https://en.wikipedia.org/wiki/Microsoft_Office_XML_formats#Excel_XML_Spreadsheet_example';
	window.open(url, '_blank');
  };
};

Option Schema:

[{"name":"href","section":"Presentation","label":"Link for button","type":"string"},{"name":"button_text","section":"Presentation","label":"Contents of button","type":"string"}]

I have noticed that when I comment the client script, the logs appear in the [syslog] table while if I do use this $scope.funcio definition, the server script is no longer running. However, I have found many articles where the use this structure to launch functions via ng-click.

 

My questions are: why this command is preventing my server script from running? Where can I find more technical information about the $scope object (because I found no reference of this object on servicenow documentation)? How could I solve my issue?

1 ACCEPTED SOLUTION

Veer
Tera Guru

@jordimsant 

Try injecting the $scope into the function definition and see if it works!

api.controller=function($scope) {
  /* widget controller */
  var c = this;
  $scope.funcio = function() {
	var url = 'https://en.wikipedia.org/wiki/Microsoft_Office_XML_formats#Excel_XML_Spreadsheet_example';
	window.open(url, '_blank');
  };
};

 

Please mark this as helpful / accepted solution if it resolves your query.

Thanks & Regards

Veer

View solution in original post

2 REPLIES 2

Veer
Tera Guru

@jordimsant 

Try injecting the $scope into the function definition and see if it works!

api.controller=function($scope) {
  /* widget controller */
  var c = this;
  $scope.funcio = function() {
	var url = 'https://en.wikipedia.org/wiki/Microsoft_Office_XML_formats#Excel_XML_Spreadsheet_example';
	window.open(url, '_blank');
  };
};

 

Please mark this as helpful / accepted solution if it resolves your query.

Thanks & Regards

Veer

jordimsant
Tera Guru

I am ashamed, it was so simple ðŸ˜‚! However, I cannot understand why this solves my problem, do you know why?