Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在同一控制器中处理不同的路由图_Javascript_Angularjs - Fatal编程技术网

Javascript 在同一控制器中处理不同的路由图

Javascript 在同一控制器中处理不同的路由图,javascript,angularjs,Javascript,Angularjs,我有一个单独的控制器,其中为每个不同的路由传递不同的参数- .when('/event/:eid/edit-question/:qid', { templateUrl: 'views/edit-question.html', controller: 'eventController', controllerAs: 'eventCtrl', resolve: { "che

我有一个单独的控制器,其中为每个不同的路由传递不同的参数-

.when('/event/:eid/edit-question/:qid', {
            templateUrl: 'views/edit-question.html',
            controller: 'eventController',
            controllerAs: 'eventCtrl',
            resolve: {
                "check": function (authService, $location) {
                    if (!authService.isLoggedIn()) {
                        $location.path('/login');
                    }
                },
                "params": function ($route) {
                    return $route.current.params;
                }
            }
        })
        .when('/event/edit-event/:eid', {
            templateUrl: 'views/edit-event.html',
            controller: 'eventController',
            controllerAs: 'eventCtrl',
            resolve: {
                "check": function (authService, $location) {
                    if (!authService.isLoggedIn()) {
                        $location.path('/login');
                    }
                },
                "params": function ($route) {
                    return $route.current.params;
                }
            }
        })
myApp.controller('eventController', ['$location','$rootScope', 'params', 'authService', 'apiService', function ($location,$rootScope, params,authService, apiService) {
 let dash = this;
//all the route parameters will be resolved and stored here
 dash.params = params;
 //get the details of an event
    dash.getTheEventDetail = () => {
        apiService.getEventDetail(dash.params.eid).then(function successCallBack(response){
           console.log(dash.params.eid);
            dash.eventDetail = response.data.data;
        });
    }
    dash.getTheEventDetail();

    //get the detail of a question for the qid passed as parameter

    dash.viewQuestion = () => {
        console.log(dash.params.qid);
        console.log(dash.eventDetail); 
        dash.questionDetail = dash.eventDetail.questions.filter(question => question._id === dash.params.qid);
        console.log(dash.questionDetail);
    }
在加载控制器之前,我正在解析路由参数。 我的控制器功能如下所示-

.when('/event/:eid/edit-question/:qid', {
            templateUrl: 'views/edit-question.html',
            controller: 'eventController',
            controllerAs: 'eventCtrl',
            resolve: {
                "check": function (authService, $location) {
                    if (!authService.isLoggedIn()) {
                        $location.path('/login');
                    }
                },
                "params": function ($route) {
                    return $route.current.params;
                }
            }
        })
        .when('/event/edit-event/:eid', {
            templateUrl: 'views/edit-event.html',
            controller: 'eventController',
            controllerAs: 'eventCtrl',
            resolve: {
                "check": function (authService, $location) {
                    if (!authService.isLoggedIn()) {
                        $location.path('/login');
                    }
                },
                "params": function ($route) {
                    return $route.current.params;
                }
            }
        })
myApp.controller('eventController', ['$location','$rootScope', 'params', 'authService', 'apiService', function ($location,$rootScope, params,authService, apiService) {
 let dash = this;
//all the route parameters will be resolved and stored here
 dash.params = params;
 //get the details of an event
    dash.getTheEventDetail = () => {
        apiService.getEventDetail(dash.params.eid).then(function successCallBack(response){
           console.log(dash.params.eid);
            dash.eventDetail = response.data.data;
        });
    }
    dash.getTheEventDetail();

    //get the detail of a question for the qid passed as parameter

    dash.viewQuestion = () => {
        console.log(dash.params.qid);
        console.log(dash.eventDetail); 
        dash.questionDetail = dash.eventDetail.questions.filter(question => question._id === dash.params.qid);
        console.log(dash.questionDetail);
    }
当我尝试访问route/event/:eid/edit question/:qid时,viewQuestion函数在getTheEventDetail之前执行,因为dash.eventDetail未定义 在“编辑问题”视图中初始化控制器时调用viewQuestion,如下所示

<div ng-init="eventCtrl.viewQuestion()"></div>


可以有一些解决方法,比如在getTheEventDetail()的末尾调用viewQuestion函数。但是这会导致每次调用getTheEventDetail时都调用viewQuestion。在这种情况下,有没有处理routeParams的好方法。

为什么不在控制器中使用$routeParams服务呢?viewQuestion似乎取决于apiService的getEventDetail方法是否成功运行并设置eventDetail。如果是这种情况,请删除ng init命令,并将视图问题添加到回拨中,以确保在对尚不存在的数据调用方法之前已完成承诺。另外,过滤器返回一个数组,由于您是按ID搜索的,所以我假设您可能需要一个问题而不是数组。如果这是正确的,您可能需要在末尾指定和索引
[0]
,或使用us
数组。请改为查找

我不确定你到底想要什么样的结果,但我在下面粘贴了一个可能的解决方案(当然没有测试)。希望有帮助

myApp.controller('eventController', ['$location','$rootScope', routeParams', 'authService', 'apiService', 
    function ($location,$rootScope, $routeParams,authService, apiService) {
     let dash = this;

     //get the details of an event
        dash.getTheEventDetail = () => {
            apiService.getEventDetail(dash.params.eid)
                .then(response => {
                    dash.eventDetail = response.data.data;
                    if ($routeParams.qid) {
                        dash.viewQuestion()
                    }
            });
        }
        dash.getTheEventDetail();

        //get the detail of a question for the qid passed as parameter

        dash.viewQuestion = () => {
            dash.questionDetail = 
                dash.eventDetail.questions.filter(question => question._id === $routeParams.qid);
            console.log(dash.questionDetail);
    }            
}