我有一個pageheader
以上的模板angularjs的UI視圖。我想指定一個變量showDropdown
來控制pageheader
中某些DOM的出現。 showDropdown
變量特定於ui-view
。例如,showDropdown
在第一個視圖中爲true
,在第二個視圖中爲false
。我如何根據ui-view改變它?如何根據ui-view設置變量?
我試圖設置onEnter()
的stateProvider
變量,但沒有影響。
HTML,
<pageheader></pageheader>
<br />
<div class="container">
<div class="row"><div class="col-xs-12 no-padding"><div ui-view="view"></div></div></div>
</div>
pageherder模板,
<div>
......
</div>
<div ng-if="showDropdown">
........
</div>
JS,
.config(function($stateProvider)
{
$stateProvider
.state('printer',
{
url: "/printer",
views:
{
"view":
{
templateUrl: "/static/tpls/cloud/app/printer.php"
}
},
onEnter: function(){
showDropdown = false;
}
})
.state('control',
{
url: "/control",
views:
{
"view":
{
templateUrl: "/static/tpls/cloud/app/control.php"
}
}
})
})
app.controller('CloudController', ['$scope', '$http', '$interval', '$timeout', 'optSev', '$state', '$rootScope', function($scope, $http, $interval, $timeout, optSev, $state, $rootScope)
{
var search = window.location.search.substring(1);
var paras = search.split("&");
paras = paras[0].split("=");
pid = paras[1];
$scope.pid = pid;
$scope.active = {};
$scope.idle = true;
$scope.active.status = {};
$scope.active.state = {};
$scope.showDropdown = $state.current.showDropdown;
//$scope.showDropdown = $rootScope.showDropdown;
//console.log($scope.showDropdown);
console.log($state);
console.log($state.current);
//console.log($state.$current);
.....
這是非常weried,
console.log($state); // it shows the showDropdown
console.log($state.current); // the showDropdown is missed.
嘗試使用$ rootScope:'$ rootScope.showDropdown = TRUE' –
@NinhPham ['當實例期間配置階段$ rootScope'不能被訪問(https://stackoverflow.com/a/10489658/6521116) –
做你想顯示/隱藏'pageheader'組件本身或其中的一部分? –