# Using Incoming Message in ui\_template Node

**URL:** https://discourse.nodered.org/t/using-incoming-message-in-ui-template-node/21069
**Category:** Dashboard
**Created:** [31 January 2020 07:55 UTC](https://discourse.nodered.org/t/using-incoming-message-in-ui-template-node/21069 "2020-01-31T07:55:56Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sesenlik](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@sesenlik](https://discourse.nodered.org/u/sesenlik)
#### Post date: [31 January 2020 07:55 UTC](https://discourse.nodered.org/t/using-incoming-message-in-ui-template-node/21069/1 "2020-01-31T07:55:56Z")

</div>

Hello everyone,

I am trying to receive incoming messages within a ui\_template node in my flow. I followed the example in info bar and I am able to receive incoming messages in ui\_template. But when I refresh the page or switch back to same page using title bar on dashboard, scope.$watch catches an old message as if a new one is received even though there is no message incoming.

Is there any way I can get around this issue? I need scope.$watch to fire only if there is really an incoming message from input of ui\_template.

Thanks for any help.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [31 January 2020 11:09 UTC](https://discourse.nodered.org/t/using-incoming-message-in-ui-template-node/21069/2 "2020-01-31T11:09:14Z")

</div>

In a case like this, it would be helpful if you posted a minimal flow showing the issue. Please read  
[How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) before posting the flow.

Also there are three examples in the info tab, which one are you refering to?

---

<div class="post-metadata">

### Author: ![sesenlik](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@sesenlik](https://discourse.nodered.org/u/sesenlik)
#### Post date: [31 January 2020 18:00 UTC](https://discourse.nodered.org/t/using-incoming-message-in-ui-template-node/21069/3 "2020-01-31T18:00:14Z")

</div>

Sorry, actually my flow was very simple that's why I did not share it. Basically I have an inject connected to a template containing below javascript

```auto
<script>
(function(scope) {
  scope.$watch('msg', function(msg) {
    if (msg) {
      console.log("Fire!");	  
	  
      $("#my_"+scope.$id).html(msg.payload);
    }
  });
})(scope);
</script>

```

When I click on inject to try it out, it works correctly and prints Fire! to console. But I noticed that Fire! is printed to console, when the dashboard page is refreshed with F5 or if I switch back to the page containing the ui\_template. I want to prevent this behaviour. Fire! should be printed only when a message is received from inject.

To be honest I've already find a solution to my problem by adding an extra property to msg and check its change in $watch event, but I did not like that solution and I want to understand why this problem occurs.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [31 January 2020 19:17 UTC](https://discourse.nodered.org/t/using-incoming-message-in-ui-template-node/21069/4 "2020-01-31T19:17:17Z")

</div>

Currently any incoming message is stored in the overall state so that that any late joining client (ui displays) will be able to get that last known state of a widget.  
There is a PR in the works to allow this to be more optional - but this hasn't translated into any option on the template to expose that option yet. (so that this behaviour could then optionally be turned off)

---

<div class="post-metadata">

### Author: ![sesenlik](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@sesenlik](https://discourse.nodered.org/u/sesenlik)
#### Post date: [1 February 2020 07:00 UTC](https://discourse.nodered.org/t/using-incoming-message-in-ui-template-node/21069/5 "2020-02-01T07:00:11Z")

</div>

Thanks for the reply. I share my solution for the problem below. Maybe it can be some use to somebody else.

I've added the cnt property to msg object. Cnt property is incremented only when the button triggering the ui\_template containing code below. Passing newValue and oldValue to callback method I am able to the current and previous state of msg object. Comparing current and previous cnt property of msg object I am able to detect if button is pressed or not.

```auto
<script>
(function(scope) {
  scope.$watch('msg', function(newValue,oldValue,scope) {
    if (newValue) {      
      if (typeof(newValue) != "undefined" && typeof(oldValue) != "undefined"){
          if (newValue.cnt != oldValue.cnt){
              console.log("Button is pressed!");            
          }
      }      
      $("#my_"+scope.$id).html(newValue.payload);
    }
  });
})(scope);
</script>

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [2 March 2020 07:07 UTC](https://discourse.nodered.org/t/using-incoming-message-in-ui-template-node/21069/6 "2020-03-02T07:07:27Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
