Node-Red UI模板:如何“固定”变量的值?

时间:2018-07-08 15:41:11

标签: node-red

经过Internet的大量尝试和研究(好吧,我是HTML / Java的新手),我设法以下面的方式对Node-Red UI-Dashboard模板进行编码,即当{{1 }}被发送,HTML被更新,变量msg.payload的值可以作为消息传递给另一个节点。但是,我注意到,当发送具有不同结构(例如value)的消息并且在浏览器中刷新UI-Dashboard时,变量msg.host消失了。因此,如何确定ist的值,以便仅在变量value中有新的msg.payload时才进行更新?

value

任何帮助将不胜感激!这是Node-Red流:

<div>{{value}}</div><br>

<md-button ng-click="send({payload:action()})">
    Click me to send a message
</md-button>

<script>
(function(scope) {
  scope.$watch('msg', function(msg) {
    if (msg.payload) {
	  scope.value = msg.payload;
	}
  scope.action = function() {
    return scope.value;
    }
  });
})(scope);
</script>

经过更多研究后,我发现我正在寻找获取新消息或刷新浏览器后保留变量值的方法:[{"id":"3f23f703.42bea8","type":"ui_template","z":"66b95f66.704eb","group":"b95933b3.c8423","name":"","order":0,"width":"6","height":"3","format":"<div>{{value}}</div><br>\n\n<md-button ng-click=\"send({payload:action()})\">\n Click me to send a message\n</md-button>\n\n<script>\n(function(scope) {\n scope.$watch('msg', function(msg) {\n if (msg.payload) {\n\t scope.value = msg.payload;\n\t}\n scope.action = function() {\n return scope.value;\n }\n });\n})(scope);\n</script>","storeOutMessages":true,"fwdInMessages":false,"templateScope":"local","x":320,"y":1440,"wires":[["2c92bfb9.3b913"]]},{"id":"2c92bfb9.3b913","type":"debug","z":"66b95f66.704eb","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":490,"y":1440,"wires":[]},{"id":"52897833.650b18","type":"inject","z":"66b95f66.704eb","name":"","topic":"","payload":"boat","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":1440,"wires":[["3f23f703.42bea8"]]},{"id":"b4427fff.dd20b","type":"inject","z":"66b95f66.704eb","name":"","topic":"","payload":"house","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":1480,"wires":[["3f23f703.42bea8"]]},{"id":"505e7106.a90bc","type":"inject","z":"66b95f66.704eb","name":"","topic":"","payload":"car","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":1520,"wires":[["3f23f703.42bea8"]]},{"id":"f0f33fe9.21da5","type":"inject","z":"66b95f66.704eb","name":"","topic":"","payload":"500","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":1560,"wires":[["3f23f703.42bea8"]]},{"id":"f857f83f.7d2108","type":"function","z":"66b95f66.704eb","name":"test msg","func":"var servers = {\n host: 'localhost',\n port: 6680,\n};\nreturn [servers];","outputs":1,"noerr":0,"x":320,"y":1380,"wires":[["3f23f703.42bea8"]]},{"id":"1ee40d04.3106d3","type":"inject","z":"66b95f66.704eb","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":1380,"wires":[["f857f83f.7d2108"]]},{"id":"b95933b3.c8423","type":"ui_group","z":"","name":"Group 2","tab":"f04b5dc7.38763","order":2,"disp":false,"width":"6","collapse":false},{"id":"f04b5dc7.38763","type":"ui_tab","z":"66b95f66.704eb","name":"New Test","icon":"dashboard"}] 。这是更新后的localStorage

<script>

1 个答案:

答案 0 :(得分:0)

执行此操作的正确方法是将输入过滤到模板节点,以使内部状态不会因没有msg.payload值的消息而被重置。

如果您仍然需要在模板节点的下游使用这些其他消息,则可以使用switch节点之类的消息将消息路由到模板节点。

context也是针对服务器端而不是浏览器端的,它在Angular / HTML / CSS空间中不起作用。