我在node-red浏览器ide上有一个mqtt节点,它正在侦听有关主题的传入消息。
我想在node-red-dashboard中填入包含该信息的列表,因此我添加了一个具有该格式的模板节点:
<ul id="messagesList"></ul>
我不仅想要添加可以通过角度绑定完成的这些信息,我还想删除旧条目,以便最多3条消息在表中。因此,我需要在每个可以访问仪表板页面的传入消息上运行javascript。
我怎样才能实现这一目标?每次收到消息时,如何运行带有访问html页面的JavaScript?
答案 0 :(得分:1)
添加函数节点,以将消息存储在流上下文中。
var message = msg.payload;
var messages = flow.get("messagesList") || [];
if(messages.length < 3) {
//Push to message list
messages.push(message);
} else {
//Remove first message and add new one
messages.splice(0,1);
messages.push(message);
}
flow.set("messagesList", messages);
msg.payload = messages;
return msg;
在dahsboard模板节点中,使用ng-repeat显示列表,例如:
<ul id="messagesList">
<li ng-repeat="x in msg.payload">{{x}}</li>
</ul>
工作示例:
[{"id":"5c713e84.dacb98","type":"function","z":"47849408.20a044","name":"","func":"var message = msg.payload;\nvar messages = flow.get(\"messagesList\") || [];\n\nif(messages.length < 3) {\n\n //Push to message list\n messages.push(message);\n} else {\n\n //Remove first message and add new one\n messages.splice(0,1);\n messages.push(message);\n}\n\nflow.set(\"messagesList\", messages);\n\nmsg.payload = messages;\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":500,"wires":[["e6bdd32f.ec218"]]},{"id":"e6bdd32f.ec218","type":"ui_template","z":"47849408.20a044","group":"77d3195c.d9af28","name":"","order":0,"width":0,"height":0,"format":"<ul id=\"messagesList\">\n <li ng-repeat=\"x in msg.payload\">{{x}}</li>\n</ul>","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":460,"y":500,"wires":[[]]},{"id":"87e7fba4.ccfa6","type":"inject","z":"47849408.20a044","name":"","topic":"","payload":"","payloadType":"date","repeat":"2","crontab":"","once":false,"x":150,"y":500,"wires":[["5c713e84.dacb98"]]},{"id":"77d3195c.d9af28","type":"ui_group","z":"","name":"Sensors","tab":"cd626a92.d20a78","disp":true,"width":"9"},{"id":"cd626a92.d20a78","type":"ui_tab","z":"","name":"Dashboard","icon":"dashboard"}]