繁体   English   中英

angularjs ui-router 嵌套状态新 controller 不工作

[英]angularjs ui-router nested states new controller not working

我被 ui-router 中的嵌套状态困住了。 我正在渲染一个包含地区、国家和每个国家的人的页面

索引.html 有三个区域作为链接,EMEA APAC 和 AMER

索引页的部分有:

<a ui-sref="territory({territory: 'amer'})">AMER</a>&nbsp;<a ui-sref="territory({territory: 'emea'})">EMEA</a>&nbsp;<a ui-sref="territory({territory: 'apac'})">APAC</a>

<ui-view></ui-view>

单击链接时,国家/地区由 $http.post 操作返回,并由 /views/countries.html 显示为:

<h1>This is countries</h1>
<br>
Teritory: {{region}}
<div ng-repeat='country in countries'>
    <a ui-sref=".support({country: '{{country}}'})">{{country}}</a ui-sref>
</div>

到目前为止,这是有效的。 所以我在每个国家都有一个.support链接,现在问题是,当我点击链接时。 state 被接受..,因为它正在加载模板URL。 但似乎未加载 state 中指定的 controller : angularjs 代码:

$urlRouterProvider.otherwise('/home');
    $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'views/home.html',
            controller: 'supportCtrl'
        })
        .state('territory', {
            url: '/territory/:territory',
            templateUrl: 'views/countries.html',
            controller: 'countryController'
        })
        .state('territory.support',{
            url: '/:country',
            templateUrl: 'views/supporter.html',
            controller: 'supportController'
        })
});

支持 controller 正在编写一个 console.log 条目......但没有任何反应。 控制器:

supportApp.controller('countryController',['$scope', 'supportService', '$location', '$stateParams', function($scope, supportService, $location, $stateParams){
    
    $scope.fromTerritory = $stateParams.territory;
    $scope.getCountries = function(){
        var countries = supportService.getCountries($scope.fromTerritory);
        countries.then(function(response){
            if(response.data.error){
                $scope.error = true;
                $scope.message = response.data.message;
            }
            else{
                console.log('response OK');
                $scope.region = $scope.fromTerritory;
                $scope.countries = response.data;
            }
        });
    }
    $scope.getCountries();

}]);

supportApp.controller('supportController',['$scope', 'supportService', '$location', '$stateParams', function($scope, supportService, $location, $stateParams){
    console.log('in the supportController');
    var test = function(){
        console.log('supportController country: '+$stateParams.country);
    }
    test();
}]);

我可能在这里做错了什么。 目标是单击国家,然后显示属于该特定国家的人的姓名。

如果不清楚,我可以在需要的地方提供更多信息。 但目前看来,嵌套 state (.territory.support) 的 controller (supportController) 没有运行/加载

谢谢你!

好的,我现在有我的答案......因为我没有在 supporter.html 上使用<ui-view></ui-view> ,controller 不是“需要的”并且没有加载。 添加 consol.log 后显示日志条目。

暂无
暂无

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

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