我正在使用ui路由器與angularjs。我想爲視圖編寫一個模板,根據視圖的內容顯示圖像。這是我的示例狀態。有我的角度控制器檢查,以查看它與ui路由器位於什麼UI-
$stateProvider
.state('index', {
url: "",
views: {
"topStormtrooper": {
templateUrl: '/components/stormtroopers/stormtroopers.html',
controller: "stormtroopersCtrl"
},
"bottomStormtrooper": {
templateUrl: '/components/stormtroopers/stormtroopers.html',
controller: "stormtroopersCtrl"
}
}
})
我的控制器看起來像這樣
.controller('stormtroopersCtrl', ['$scope', '$http', '$stateParams', function ($scope, $http, $stateParams) {
//
$scope.stormtrooper = $stateView; //it should be something like this hopefully
}]);
模板都是一樣的只是圖像將根據其查看被加載到不同的。目前我只是爲每個視圖添加了一個新的控制器,並基於該控制器加載圖像。但我覺得我應該可以用一個控制器來做到這一點,並且控制器應該能夠檢測到視圖是什麼。我知道你可以檢測到這個狀態,但是我想要更深入地瞭解這個視圖。
任何想法?
您是否想要訪問控制器目前的狀態?像 'console.log($ scope.stormtrooper)//「index」' –