[英]Why I can't just access the root-scope variable in the HTML's <script> </script> part?
我是AngularJs的新手,並且正在構建一個簡單的AngularJs網站,我已經設置了頁面,並且該頁面可以正常運行。 但是,當我嘗試在index.html的腳本部分中使用rootscope變量時。 它總是給出一個錯誤:該變量未定義。 但是,我仍然可以在index.html的html部分中使用該變量。 誰能告訴我為什么它會這樣工作? 以及如何解決? 謝謝。 這是我簡單的HTML代碼:
<!DOCTYPE html>
<html data-ng-app="test" lang='laCo'>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge; IE=10; IE=9">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet"
href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
loaddivpage();
});
loaddivpage = function(){
window.Final = user.userId;
$( "#innerdiv" ).load("./app/chat/Dialog.html");
}
</script>
</head>
<body>
<div id ="webdiv">
<div id = "innerdiv"> </div>
</div>
</body>
</html>
在這里,我想將Dialog.html加載到使用window.Final變量的innerdiv中。 注意:user.userId是rootscope變量。
我設法找到一種方法,因為html部分與根范圍有關,因此我可以使用angular來獲取根范圍變量:像:通過使用angular.element($(“#your div id”)) .scope()獲取html部分的范圍,然后使用此范圍訪問根范圍變量。 希望這會幫助別人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.