简体   繁体   English

让我的角度控制器检查一下它在ui-router中的位置

[英]have my angular controller check to see what ui-view it's located in with ui-router

I'm using ui-router with angularjs. 我正在将ui-router与angularjs一起使用。 I want to write a template for a view that will show a image depending on what the view is. 我想为视图编写一个模板,该模板将根据视图的内容显示图像。 here's my example state. 这是我的示例状态。

$stateProvider
    .state('index', {
        url: "",
        views: {

             "topStormtrooper": { 
                templateUrl: '/components/stormtroopers/stormtroopers.html',
                controller: "stormtroopersCtrl"
            },

             "bottomStormtrooper": { 
                templateUrl: '/components/stormtroopers/stormtroopers.html',
                controller: "stormtroopersCtrl"
            }
        }
    })

my controller looks like this 我的控制器看起来像这样

.controller('stormtroopersCtrl', ['$scope', '$http', '$stateParams', function ($scope, $http, $stateParams) {
//   
$scope.stormtrooper = $stateView; //it should be something like this hopefully


}]);

The template is all the same just the image will be different depending which view it is loaded into. 模板完全相同,只是图像会有所不同,具体取决于将其加载到哪个视图中。 Currently I just added a new controller for each view and load the image based on that. 目前,我只是为每个视图添加了一个新的控制器,并基于此加载了图像。 But I feel like I should be able to do this with just one controller and the controller should be able to detect what the view is. 但是我觉得我应该只用一个控制器就能做到这一点,而且控制器应该能够检测出视图是什么。 I know you can detect the state but I want go deeper and get the view. 我知道您可以检测到状态,但是我想更深入地了解。

Any Ideas? 有任何想法吗?

You can access the current state configuratin object like this: 您可以像这样访问当前状态的配置对象:

$state.current

For further information take a look at the $state 有关更多信息,请查看$ state

You can listen to the $viewContentLoaded function in your controller as per the ui-router documentation 您可以根据ui-router文档在控制器中收听$viewContentLoaded函数

For example: 例如:

.controller('stormtroopersCtrl', ['$scope', '$http', '$stateParams', function ($scope, $http, $stateParams) {

$scope.$on("$viewContentLoaded",function(event,viewName){ //listen for when the content is loaded into the view
    $scope.currentView = viewName;  //assign the view name in a model
    console.log($scope.currentView);
    //do something because you got the view name now....

});

}]);

You do not need to change your state just for an image in template. 您无需仅为模板中的图像更改状态。 You can make it with scope variables. 您可以使用范围变量来实现。

 angular.module('app',[]) .controller('stormtrooperCtrl', ['$scope', function ($scope) { // $scope.selected = 0; $scope.images = [ "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSz6dBOOsFVAeSilVEIO9dqwrY4R5gCzEMcrRVZguhYhr9PVJsThQ", "http://www.wallpapereast.com/static/images/Wallpaper-Nature-8B71.jpg", "http://www.intrawallpaper.com/static/images/1250654-for-laptop-nature.jpg" ]; }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> <div ng-app="app"> <div ng-controller="stormtrooperCtrl"> <button ng-click="selected=0">image1</button> <button ng-click="selected=1">image2</button> <button ng-click="selected=2">image3</button> <div> <img width="100" ng-src="{{images[selected]}}"/> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM