sputil.get()

David Pichard
Mega Guru

Hi all, I'm trying to dynamically load widgets in service portal based on a broadcast event. I am unable to get the following code to load a widget. The "broadcast" and "on" functions work as expected as I can log the actual string value of the widget to load. I can also log the get() functions response which is an object, I just can't load the new widget into the container. Any ideas?:

//Client script:
$rootScope.$on('updateWidget', function (event, widget) {
  spUtil.get(widget).then(function(response) {
    c.container = response;
  });

});

 

//HTML:
<div>
<sp-widget widget="c.container"></sp-widget>
</div>

1 ACCEPTED SOLUTION

I believe the scope does change if you console.log out the widget info. However, it doesn't apply the newly suggested widget. 
One way around this is to use an array. For example:
Client Script:


$rootScope.$on('updateWidget', function (event, widget) {
  spUtil.get(widget).then(function(response) {
    c.containers = [response];

});

HTML:

<div ng-repeat="container in c.containers">
    <sp-widget widget="container"></sp-widget>
</div>

The ngRepeat directive will now update the view as the array changes, albeit just one item.

 

View solution in original post

6 REPLIES 6

Prasun
Giga Guru

Hi David,

 

have you tried putting ng-if and the condition will be triggered by the broadcast function of the other widget?

 

Regards

Prasun

Hi Prasun, thanks for the response. The event is triggering ok as is, it's just that the widget is not being updated. Kinda like the inner function doesn't have scope access to c.container

I believe the scope does change if you console.log out the widget info. However, it doesn't apply the newly suggested widget. 
One way around this is to use an array. For example:
Client Script:


$rootScope.$on('updateWidget', function (event, widget) {
  spUtil.get(widget).then(function(response) {
    c.containers = [response];

});

HTML:

<div ng-repeat="container in c.containers">
    <sp-widget widget="container"></sp-widget>
</div>

The ngRepeat directive will now update the view as the array changes, albeit just one item.

 

Much thanks ChrisB, that did the trick!!