Using ng-repeat in UI page

eackley
Tera Contributor

I am creating a custom PIR report UI Page. I have a gliderecord object which fetches related cmdb items from a major incident. I would like to use ng-repeat to print the records in a nicely formatted table. I am having trouble using ng-repeat in my html body to do this and would like someone to explain how to use an array object that has not been hardcoded with values.

 

I have tried below:

(Attempted script but also have tried "g:evaluate" the incident sys id is hardcoded for testing purposes and the line above the ng-repeat is also for testing purposes to)

 
<script>
        var app = angular.module('myApp', []);
        app.controller('myController', function($scope) {
            $scope.assetDetail = [];
        ciGr = new GlideRecord('task_ci');
        ciGr.get('task', '89b995829730e2100ea0385ef053afbe');
        $scope.assetDetail.push(ciGr.getDisplayValue('ci_item.name'));
        while(ciGr.next()){
            $scope.assetDetail.push(ciGr.getDisplayValue('ci_item.name'));
        }
        });
    </script>
 <div ng-controller="myController">
        <h2>List of Items</h2>
        <ul> <li> ${assetDetails.pop().toString()}</li> </ul>
        <ul>
            <li ng-repeat="item in assetDetails">
                {{item}}
            </li>
        </ul>
 
1 ACCEPTED SOLUTION

pratikjagtap
Giga Guru

Hi @eackley ,

 

You need to:

  • Get data using GlideRecord in the server-side script block of the UI Page.

  • Assign it to g_scratchpad.

  • Parse it into $scope in the Angular controller.

1. UI Page Server Script (Script section):

var assetDetail = [];

var ciGr = new GlideRecord('task_ci');
ciGr.addQuery('task', '89b995829730e2100ea0385ef053afbe'); // your major incident/task sys_id
ciGr.query();

while (ciGr.next()) {
var item = ciGr.ci_item.name.toString(); // or ciGr.getDisplayValue('ci_item')
assetDetail.push(item);
}

g_scratchpad.assetDetail = JSON.stringify(assetDetail);

 

2. HTML + AngularJS Controller:

<div ng-app="myApp" ng-controller="myController">
<h2>List of CI Items</h2>
<ul>
<li ng-repeat="item in assetDetail">
{{item}}
</li>
</ul>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
$scope.assetDetail = JSON.parse($g_scratchpad.assetDetail);
});
</script>

 

If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Pratik

View solution in original post

2 REPLIES 2

pratikjagtap
Giga Guru

Hi @eackley ,

 

You need to:

  • Get data using GlideRecord in the server-side script block of the UI Page.

  • Assign it to g_scratchpad.

  • Parse it into $scope in the Angular controller.

1. UI Page Server Script (Script section):

var assetDetail = [];

var ciGr = new GlideRecord('task_ci');
ciGr.addQuery('task', '89b995829730e2100ea0385ef053afbe'); // your major incident/task sys_id
ciGr.query();

while (ciGr.next()) {
var item = ciGr.ci_item.name.toString(); // or ciGr.getDisplayValue('ci_item')
assetDetail.push(item);
}

g_scratchpad.assetDetail = JSON.stringify(assetDetail);

 

2. HTML + AngularJS Controller:

<div ng-app="myApp" ng-controller="myController">
<h2>List of CI Items</h2>
<ul>
<li ng-repeat="item in assetDetail">
{{item}}
</li>
</ul>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
$scope.assetDetail = JSON.parse($g_scratchpad.assetDetail);
});
</script>

 

If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Pratik

eackley
Tera Contributor

@pratikjagtap Thank you for the response! Is the server side script of a UI page the same as the processing script? I attempted to use the solution you provided but it does not seem to use the ng-controller function. I think I may have done something incorrectly with the server side script.

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<html>
<head>

</head>
    <div ng-app="myApp" ng-controller = "myController">
        <h2>List of Items</h2>
        <ul>
	<li ng-repeat="item in assetDetails">
		{{item}}
	</li>
		</ul>


    </div>
<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
$scope.assetDetails = JSON.parse($g_scratchpad.assetDetails);
});
</script>


</html>
</j:jelly>