Javascript 将js对象传递给angularjs
我正在尝试创建自己的仪表板,以显示Home Assistant服务器提供的数据。鉴于下面的脚本,我已经很好地工作了,我如何从Javascript 将js对象传递给angularjs,javascript,angularjs,home-assistant,Javascript,Angularjs,Home Assistant,我正在尝试创建自己的仪表板,以显示Home Assistant服务器提供的数据。鉴于下面的脚本,我已经很好地工作了,我如何从subscribeEntities(连接、实体)传递输出到angularjs而不是renderEntities函数?目标是能够使用angularjs表达式用数据填充我的仪表板。根据这里的例子,我认为订阅实体(连接、实体)提供了一个js对象,但我不是100%确定 谢谢你的帮助。任何帮助或提示都将不胜感激 猴面包树 <html> <body>
subscribeEntities(连接、实体)传递输出代码>到angularjs而不是renderEntities
函数?目标是能够使用angularjs表达式用数据填充我的仪表板。根据这里的例子,我认为订阅实体(连接、实体)
提供了一个js对象,但我不是100%确定
谢谢你的帮助。任何帮助或提示都将不胜感激
猴面包树
<html>
<body>
<table>
<tbody></tbody>
</table>
<script type="module">
import {
getAuth,
getUser,
callService,
createConnection,
subscribeEntities,
ERR_HASS_HOST_REQUIRED
} from "./dist/index.js";
(async () => {
let auth;
try {
auth = await getAuth();
} catch (err) {
if (err === ERR_HASS_HOST_REQUIRED) {
const hassUrl = prompt(
"What host to connect to?",
"http://ip-of-ha:8123"
);
if (!hassUrl) return;
auth = await getAuth({ hassUrl });
} else {
alert(`Unknown error: ${err}`);
return;
}
}
const connection = await createConnection({ auth });
subscribeEntities(connection, entities =>
renderEntities(connection, entities)
);
// Clear url if we have been able to establish a connection
if (location.search.includes("auth_callback=1")) {
history.replaceState(null, "", location.pathname);
}
// To play from the console
window.auth = auth;
window.connection = connection;
getUser(connection).then(user => {
console.log("Logged in as", user);
window.user = user;
});
})();
function renderEntities(connection, entities) {
const root = document.querySelector("tbody");
while (root.lastChild) root.removeChild(root.lastChild);
Object.keys(entities)
.sort()
.forEach(entId => {
const tr = document.createElement("tr");
const tdName = document.createElement("td");
tdName.innerHTML = entId;
tr.appendChild(tdName);
const tdState = document.createElement("td");
const text = document.createTextNode(entities[entId].state);
tdState.appendChild(text);
if (
["switch", "light", "input_boolean"].includes(
entId.split(".", 1)[0]
)
) {
const button = document.createElement("button");
button.innerHTML = "toggle";
button.onclick = () =>
callService(connection, "homeassistant", "toggle", {
entity_id: entId
});
tdState.appendChild(button);
}
tr.appendChild(tdState);
root.appendChild(tr);
});
}
</script>
</body>
</html>
进口{
getAuth,
getUser,
呼叫服务,
创建连接,
认购人,
错误\u需要主机\u
}来自“/dist/index.js”;
(异步()=>{
让我们来验证;
试一试{
auth=等待getAuth();
}捕捉(错误){
if(err==err\u HASS\u主机\u必需){
const hassUrl=prompt(
“要连接到哪个主机?”,
"http://ip-of-ha:8123"
);
如果(!hassUrl)返回;
auth=await getAuth({hassUrl});
}否则{
警报(`Unknown error:${err}`);
返回;
}
}
const connection=await createConnection({auth});
订阅实体(连接、实体=>
渲染性(连接、实体)
);
//如果我们能够建立连接,请清除url
if(location.search.includes(“auth\u callback=1”)){
replaceState(null,“,location.pathname);
}
//从控制台播放
window.auth=auth;
window.connection=连接;
getUser(连接)。然后(用户=>{
console.log(“以用户身份登录”);
window.user=用户;
});
})();
函数呈现(连接、实体){
const root=document.querySelector(“tbody”);
而(root.lastChild)root.removeChild(root.lastChild);
对象。键(实体)
.sort()
.forEach(entId=>{
const tr=document.createElement(“tr”);
const tdName=document.createElement(“td”);
tdName.innerHTML=entId;
tr.appendChild(tdName);
const tdState=document.createElement(“td”);
const text=document.createTextNode(实体[entId].state);
tdState.appendChild(文本);
如果(
[“开关”、“灯”、“输入布尔值”]。包括(
实体分割(“.”,1)[0]
)
) {
const button=document.createElement(“按钮”);
button.innerHTML=“切换”;
button.onclick=()=>
呼叫服务(连接,“家庭助理”,“切换”{
实体id:entId
});
tdState.appendChild(按钮);
}
tr.appendChild(tdState);
根。附属物(tr);
});
}