# Setting a unique ID for a node

**URL:** <https://discourse.nodered.org/t/setting-a-unique-id-for-a-node/70169>\
**Category:** General\
**Created:** [6 November 2022 14:59 UTC](https://discourse.nodered.org/t/setting-a-unique-id-for-a-node/70169 "2022-11-06T14:59:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andro/32/70042_2.png) [@andro](https://discourse.nodered.org/u/andro)\
**Post date:** [6 November 2022 14:59 UTC](https://discourse.nodered.org/t/setting-a-unique-id-for-a-node/70169/1 "2022-11-06T14:59:43Z")

</div>

The help for the template widget states:

```auto
<div id="{{'my_'+$id}}" style="{{'color:'+theme.base_color}}">Some text</div>
<script>
(function(scope) {
  scope.$watch('msg', function(msg) {
    if (msg) {
      // Do something when msg arrives
      $("#my_"+scope.$id).html(msg.payload);
    }
  });
})(scope);
</script>

```

I do not understand where the $id variable comes from or what it is. A google search revealed nothing for me. Reading the node red help online also led nowhere. I am missing something fundamental. Any pointers would be most appreciated.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 November 2022 16:54 UTC](https://discourse.nodered.org/t/setting-a-unique-id-for-a-node/70169/2 "2022-11-06T16:54:05Z")

</div>

> <https://stackoverflow.com/questions/30434493/angularjs-how-is-a-scope-ids-value-set-doesnt-seem-to-be-incremental>

It is a number that identifies the current scope of the template.

Say you have 2 or them with identical code - you use it to distinguish which "scope" (template) you are in.

in JavaScript, you access it via `scope.$id` in the HTML via $id or `{{$id}}`

So if `scope.$id` is `123` then `id="{{'my_'+$id}}"` becomes `id="my_123"`

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [6 November 2022 21:44 UTC](https://discourse.nodered.org/t/setting-a-unique-id-for-a-node/70169/3 "2022-11-06T21:44:14Z")

</div>

Just for my own peace of mind, as I understand it `scope.$id` generates its own value, which is then used as the `$id` in `"{{'my_'+$id}}"` and trying to use `scope.$id` and `$id` in the body without the moustache is a bad idea.

Please let me know if this is wrong

---

<div class="post-metadata">

**Author:** ![andro](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andro/32/70042_2.png) [@andro](https://discourse.nodered.org/u/andro)\
**Post date:** [6 November 2022 22:49 UTC](https://discourse.nodered.org/t/setting-a-unique-id-for-a-node/70169/4 "2022-11-06T22:49:37Z")

</div>

@Steve-Mcl thank you for that. So this is Angular then is it? I am very new to node-red and unfamiliar with Angular. Are you able to point me to any resources for learning about how Angular works together with node red? I was unable to find anything much on the node red documentation website.

Although I have been a programmer for forty years (!) I have never used Angular - believe it or not. 🙂

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 November 2022 23:51 UTC](https://discourse.nodered.org/t/setting-a-unique-id-for-a-node/70169/5 "2022-11-06T23:51:38Z")

</div>

> [@andro](#):
>
> So this is Angular then is it

No, to be clear this is angularjs. angular is the successor of angularjs .

I am not an expert in angularjs (or angular for that matter) but there is tonnes of info around the net. Just be sure whatever tutorials or sample code you read/use is angularjs.

---

<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:** [5 January 2023 23:51 UTC](https://discourse.nodered.org/t/setting-a-unique-id-for-a-node/70169/6 "2023-01-05T23:51:48Z")

</div>

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