<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Calling widget's client controller from Body HTML script tag in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2712984#M1048330</link>
    <description>&lt;P&gt;I've the following in the Body HTML template of widget.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Nisar2_0-1698236053729.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/299984iA53FE1E30E7E4CCD/image-size/medium?v=v2&amp;amp;px=400" alt="Nisar2_0-1698236053729.png" title="Nisar2_0-1698236053729.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The alert is working. But I've not been able to figure out how to pass that info to the client controller or server side. Any suggestions?&lt;/P&gt;</description>
    <pubDate>Wed, 25 Oct 2023 12:15:56 GMT</pubDate>
    <dc:creator>Nisar2</dc:creator>
    <dc:date>2023-10-25T12:15:56Z</dc:date>
    <item>
      <title>Calling widget's client controller from Body HTML script tag</title>
      <link>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2712984#M1048330</link>
      <description>&lt;P&gt;I've the following in the Body HTML template of widget.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Nisar2_0-1698236053729.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/299984iA53FE1E30E7E4CCD/image-size/medium?v=v2&amp;amp;px=400" alt="Nisar2_0-1698236053729.png" title="Nisar2_0-1698236053729.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The alert is working. But I've not been able to figure out how to pass that info to the client controller or server side. Any suggestions?&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 12:15:56 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2712984#M1048330</guid>
      <dc:creator>Nisar2</dc:creator>
      <dc:date>2023-10-25T12:15:56Z</dc:date>
    </item>
    <item>
      <title>Re: Calling widget's client controller from Body HTML script tag</title>
      <link>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2713757#M1048552</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions for this?&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 03:48:06 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2713757#M1048552</guid>
      <dc:creator>Nisar2</dc:creator>
      <dc:date>2023-10-26T03:48:06Z</dc:date>
    </item>
    <item>
      <title>Re: Calling widget's client controller from Body HTML script tag</title>
      <link>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2714600#M1048814</link>
      <description>&lt;P&gt;I have never tried pulling in React in Service Portal, but the following might work.&lt;/P&gt;&lt;P&gt;In the client controller define some variables on $scope or c.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;api.controller = function($scope){
    var c = this;
    $scope.geoLoc = {
        "lat": "",
        "long": ""
    };
    
    //to test if this works watch the variables for changes and log out values
    $scope.$watch( 'geoLoc ', function (newValue, oldValue ) {
        if(newValue.lat || newValue.long)
            console.log(newValue)
    }, true)
...
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since your React script is outside AngularJS scope you'll need to grab it within the script tags in the HTML template. You'll also want to wrap within a setTimeout to ensure the elements are present before the script runs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;  &amp;lt;script&amp;gt;
    
    setTimeout(function(){
        var scope = angular.element('#Geo_Map_root').scope();
        let locTimer;
        const loadLocationMap = () =&amp;gt; {
          const { LocationMap } = window.geoJsLib;
          const root = ReactDOM.createRoot(document.getElementById('Geo_Map_root'));
          const onSubmit=(location) =&amp;gt; {
            scope.geoLoc.lat = location.latitude.toString();
            scope.geoLoc.long = location.longitude.toString();
          }
          root.render(React.createElement(LocationMap, { onSubmit }, null));
        }
    },500);
    
  &amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know that doing this works without using React, so theoretically it should work with it.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="react_test.gif"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/300395i1E787B6D06D9F8FB/image-size/large?v=v2&amp;amp;px=999" alt="react_test.gif" title="react_test.gif" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 15:07:26 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2714600#M1048814</guid>
      <dc:creator>ChrisBurks</dc:creator>
      <dc:date>2023-10-26T15:07:26Z</dc:date>
    </item>
    <item>
      <title>Re: Calling widget's client controller from Body HTML script tag</title>
      <link>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2715489#M1049087</link>
      <description>&lt;P&gt;Brilliant answer. It does work. However, it takes a good 10-15 seconds for the watch() to trigger (after onSubmit() function is executed)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is that normal or can something be done about it? I've written the code to send that info to server side but given that it's taking a long time to trigger, I'm wondering if that would be a good UX.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2023 09:38:50 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2715489#M1049087</guid>
      <dc:creator>Nisar2</dc:creator>
      <dc:date>2023-10-27T09:38:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calling widget's client controller from Body HTML script tag</title>
      <link>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2715665#M1049141</link>
      <description>&lt;P&gt;-&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2023 13:23:29 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2715665#M1049141</guid>
      <dc:creator>chrisism</dc:creator>
      <dc:date>2023-10-27T13:23:29Z</dc:date>
    </item>
    <item>
      <title>Re: Calling widget's client controller from Body HTML script tag</title>
      <link>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2715696#M1049148</link>
      <description>&lt;P&gt;Watch() triggers when the value is actually changed on the variables it's watching. I have never used React mix with AngularJS and in the Service Portal; so I don't know if that is the issue. However, the watch() isn't needed unless you really are needing it to do something once the value changes. I put that in the script just to have it console log out.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2023 13:31:31 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2715696#M1049148</guid>
      <dc:creator>ChrisBurks</dc:creator>
      <dc:date>2023-10-27T13:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: Calling widget's client controller from Body HTML script tag</title>
      <link>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2716878#M1049451</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/221087"&gt;@ChrisBurks&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;&lt;SPAN&gt;However, the watch() isn't needed unless you really are needing it to do something once the value changes&lt;/SPAN&gt;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Unfortunately, I do need something to happen once the lat/long is selected. I make a call to the server script to update those values in the backend. So I'm assuming $watch() is the only way to do it in this case?&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 04:06:55 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2716878#M1049451</guid>
      <dc:creator>Nisar2</dc:creator>
      <dc:date>2023-10-30T04:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calling widget's client controller from Body HTML script tag</title>
      <link>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2717456#M1049655</link>
      <description>&lt;P&gt;No, $watch() is not the only way. When the script reaches into the scope, if the call to the server script is created on $scope or c, then it is accessible just like the other variables. In this case don't use the $watch(), just pass the data straight to the script that calls to the server.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example I'll replace the $watch() with a script that calls and passes data to the server.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;api.controller = function($scope){
    var c = this;
    $scope.geoLoc = {
        "lat": "",
        "long": ""
    };
    
    //pass the longitude and latitude to server
    c.doAnUpdate = function (geoLoc) {
        c.server.get({
            "action":"UPDATE",
            "geoLoc": geoLoc
        })
    }
   //a .then() can be used after teh c.server.get() if you need to bring something back to the client after the server has done. it's job
...
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now in the HTML &amp;lt;script&amp;gt; use that function&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;  &amp;lt;script&amp;gt;
    
    setTimeout(function(){
        var scope = angular.element('#Geo_Map_root').scope();
        let locTimer;
        const loadLocationMap = () =&amp;gt; {
          const { LocationMap } = window.geoJsLib;
          const root = ReactDOM.createRoot(document.getElementById('Geo_Map_root'));
          const onSubmit=(location) =&amp;gt; {
            scope.geoLoc.lat = location.latitude.toString();
            scope.geoLoc.long = location.longitude.toString();
            //New line added below
            scope.c.doAnUpdate(scope.geoLoc);
          }
          root.render(React.createElement(LocationMap, { onSubmit }, null));
        }
    },500);
    
  &amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Server Script:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;(function() {
  
	if(input &amp;amp;&amp;amp; input.action == "UPDATE"){
		//do what is needed with your values input.geoLoc
	}
})();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 12:59:50 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2717456#M1049655</guid>
      <dc:creator>ChrisBurks</dc:creator>
      <dc:date>2023-10-30T12:59:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calling widget's client controller from Body HTML script tag</title>
      <link>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2718305#M1049892</link>
      <description>&lt;P&gt;That's great. Thanks. I just wan't sure if I could pass that from the &amp;lt;script&amp;gt; tag. Looks like we can.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 07:52:35 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calling-widget-s-client-controller-from-body-html-script-tag/m-p/2718305#M1049892</guid>
      <dc:creator>Nisar2</dc:creator>
      <dc:date>2023-10-31T07:52:35Z</dc:date>
    </item>
  </channel>
</rss>

