我來自Angular9,真的很習慣它,而我被要求在AngularJS專案上做一些作業,我從來沒有經歷過。所以我真的很糾結整個應用程式的結構。 我的問題很簡單。我有一個sub-navbar.html模板,使用ng-include直接注入到我的root.html模板中,我想用ng-if來控制這個sub-navbar的一個部分的顯示(不是直接隱藏這個部分,我根本不希望它存在)。 我有一個后臺呼叫,它根據連接的用戶是否能看到該部分向我發送一個布林值。
我遇到的問題是,即使布林值為 "真",我的部分實際上也沒有被激活。
我試著做了一些事情:
我試著做了一些事情。
我試過的方法:
我試過的方法:
我試過的方法:
我試過的方法
很遺憾,我不能復制我所有的代碼,但以下是我正在處理的主要部分:
app.js: (我在'.run()'函式中沒有寫任何關于'$rootScope.adminSection'的內容,也嘗試了同樣的方法,直接呼叫服務,沒有'$onInit') $urlRouterProvider.otherwise("/orders") 。
$stateProvider.
.state('root', {
abstract: true,
網址。''。
views: {
'': {
TemplateUrl: "view/root.html"。
控制器。['$rootScope', 'AdministratorService', function($rootScope, AdministratorService)。{
const vm = this;
vm.$onInit = function() {
AdministratorService.getAdminSection().then(function (result) {
$rootScope.adminSection = result;
}
)
};
}]
}
}])
root.html:
< div ui-view="root_header"></div>
<div class=" row" style="min-height: 600px">
<div class="col-md-2"/span>>
<br/>/span>
<div ng-include="'view/subnavbar. html'"></div>
</div>/span>
<div class="col-md-10">
<div ui-view></div>
</div>/span>
</div>/span>
<div ng-include="'view/footer. html'"></div>
subnavbar.html:
< ul class="nav nav-pills nav-stacked" role="tablist">
<li></li>
<li ui-sref-active="active"/span>> <a ui-sref="root. 合同"></a>< div class='arrow'/span> aria-hidden='true'/span>> </div>>
</li>
<li ng-if="$rootScope. adminSection" ui-sref-active="active"> <a ui-sref="root. administrator">
</a>< div class='arrow'/span> aria-hidden='true'/span>> </div></li>
<li ui-sref-active="active"/span>> <a ui-sref="root. users"></a>< div class='arrow' aria-hidden='true'>
</div>/span></li>
</ul>
歡迎任何幫助,預先感謝!
uj5u.com熱心網友回復:
這有幾種方法可以作業。現在不作業的原因是模板中的$rootScope沒有被定義。試著在你的模板中用$rootScope.adminSection代替adminSection。
ngInclude指令創建了一個新的范圍,它繼承了$rootScope。因此,$rootScope中的變數應該可以直接從模板中訪問。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/307482.html
標籤:
上一篇:如何從陣列中一次獲得一個物件?
下一篇:如何在點擊按鈕時呼叫一個函式?
