The CreatorCon Call for Content is officially open! Get started here.

Using AngualrJS/REST in UI Macro

j_martin
Tera Guru

I am new to AngularJS as well as the REST API.   I wanted to create a responsive knowledge search using AngularJS.   The following code works within a UI page, but not in the UI macro I need in order to put it on a form. I am thinking it has to do with REST calls, but any input would be helpful.

<tr>

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

  <g:requires name="angularjs.min.1.3.14.jsdbx" />  

  <script>

  var myApp = angular.module('descEditor', []);

  myApp.controller('KnowledgeSearch', [

      '$scope',

      '$http',

      function($scope, $http) {

          $scope.knowledge = "";

          $scope.url = '/api/now/table/kb_knowledge';

          $http.defaults.headers.common.Accept = "application/json";

         

          $scope.updateList = function(knowledge) {

              $http({

                  method: 'GET',

                  url: $scope.url + "?sysparm_query=short_descriptionCONTAINS"+knowledge+"^workflow_state=published",

              }).

              success( function(data, status) {

                  $scope.descriptions = data.result;

              }).

              error ( function(data, status) {

                  $scope.descriptions = [{"knowledge": "Error fetching list"}];

              });

          }

      }

  ] );

  </script>

      <div ng-app="descEditor">

          <div ng-controller="KnowledgeSearch">

              <table><tr>

                      <p>Knowledge Search</p>

                      <input ng-model="knowledge" ng-change="updateList(knowledge);"/>

                      <div ng-repeat="desc in descriptions"><a href="kb_knowledge.do?sys_id={{desc.sys_id}}">{{desc.short_description}}</a></div>

              </tr></table>

          </div>

      </div>

</j:jelly>

</tr>

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Justin,



I tested the following in a fuji instance by adding it to a ui macro which I displayed in a formatter on the incident form and it worked for me. I think the main difference is that I removed your table/tr tags from inside the divs.



<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


  <!--<g:requires name="angularjs.min.1.3.14.jsdbx" /> -->


  <script>https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>


  <script>


  var myApp = angular.module('descEditor', []);


  myApp.controller('KnowledgeSearch', [


      '$scope',


      '$http',


      function($scope, $http) {


          $scope.knowledge = "";


          $scope.url = '/api/now/table/kb_knowledge';


          $http.defaults.headers.common.Accept = "application/json";


       


          $scope.updateList = function(knowledge) {


              $http({


                  method: 'GET',


                  url: $scope.url + "?sysparm_query=short_descriptionCONTAINS"+knowledge+"^workflow_state=published",


              }).


              success( function(data, status) {


                  $scope.descriptions = data.result;


              }).


              error ( function(data, status) {


                  $scope.descriptions = [{"knowledge": "Error fetching list"}];


              });


          }


      }


  ] );


  </script>


<tr>


      <div style="display:block;" ng-app="descEditor">


          <div ng-controller="KnowledgeSearch">


                      <p>Knowledge Search</p>


                      <input ng-model="knowledge" ng-change="updateList(knowledge);"/>


                      <div ng-repeat="desc in descriptions"><a href="kb_knowledge.do?sys_id={{desc.sys_id}}">{{desc.short_description}}</a></div>


          </div>


      </div>


  </tr>


</j:jelly>


View solution in original post

8 REPLIES 8

Brad - Thank you very much!



You were correct.   It had to do with the formatting.   It seems silly, but I assume it has to do with the way it is inserted into the form.



Thanks again.


- Justin


Brad,



I've been using Angular in a UI Formatter in Helsinki, but we just upgraded to Istanbul and it appears to be broken. I've tried several things to get around it, but haven't had any luck.   Have you or anyone else here in the community found a way around this?   If anyone does find a way, please post it.



Thanks,



Shannon


Thanks Brad! Came here with a similar issue of my angular macro not working on a form (angular was loaded correctly, but my ng-repeat on a tr tag didn't yield any rows) and would have never thought to remove the table HTML. Very odd stuff.

I'm having the same issue but I need the table as I want to display some kind of copy of the ServiceNow's tables for inputting data.... So I'm assuming there is no way of getting this to work without getting rid of the tables?