【问题标题】:routeProvider not working correctlyrouteProvider 无法正常工作
【发布时间】:2014-01-08 10:31:03
【问题描述】:

我使用 angularJS 来设置登录后我希望页面转到的位置,但如果我只有这个

function routeProviderFunction($routeProvider){
$routeProvider.when('/default',{
    templateUrl: 'HTML/login.html',
    controller: funct2
});

$routeProvider.otherwise({
    redirectTo: '/default'
});
}

我的路由提供者在上面的代码中工作,但是在我把它变成这个之后

    function routeProviderFunction($routeProvider){
$routeProvider.when('/default',{
    templateUrl: 'HTML/login.html',
    controller: funct2
});
$routeProvider.when('/adminMenu/:username', {
     templateUrl: 'HTML/adminMenu.html',
     controller: adminMenu
     });


$routeProvider.otherwise({
    redirectTo: '/default'
});
}

我的路线提供者完全停止工作。任何想法

【问题讨论】:

    标签: angularjs route-provider


    【解决方案1】:

    尝试这样做:

    $routeProvider
                .when('/default', {
                    templateUrl: 'HTML/login.html',
                    controller : 'funct2'
                }).when('/adminMenu/:username', {
                    templateUrl: 'HTML/adminMenu.html',
                    controller : 'adminMenu'
                }).otherwise({
                    redirectTo : '/default'
                });
    

    【讨论】:

    • 谢谢你这工作,现在是新问题。当我尝试使用控制器 adminMenu 时,我得到一个未定义的 adminMenu,即使它在链接到 adminMenu.html 的 js 文件中定义?
    • 您需要在您的 index html 页面中导入您的控制器 js 文件。例如:我有一个 index.html,其中有一个 div 容器,我在其中放置带有 ng-view 属性的视图,在此页面中,我导入了我需要的每个 js,例如控制器
    • 就像将它添加到其他加载的脚本中一样?
    • 因为如果我将它添加到脚本区域它仍然不会加载
    【解决方案2】:

    控制器 adminMenu 是否存在于全局命名空间中?按 F12 并检查错误控制台。 Angular 有相当不错的错误信息。另外值得注意的是'when'是可链接的,所以你应该做这样的事情 route.when().when().otherwise();

    【讨论】:

      【解决方案3】:

      查看@fabuloso 的答案后,它点击了我需要解决的问题。

      我有

      pageApp.config(function($routeProvider) {
          $routeProvider
      
              // route for the home page
              .when('/', {
                  templateUrl : 'content/home.html',
                  controller  : 'homeController'
              })
      
              // route for the directions page
              .when('/directions', {
                  templateUrl : 'content/directions.html',
                  controller  : 'directionsController'
              })
      
              // route for the giftCards page
              .when('/giftCards', {
                  templateUrl : 'content/giftCards.html',
                  controller  : 'giftCardsController'
              });
      
              // route for the giftCards page
              .when('/contactUs', {
                  templateUrl : 'content/contactUs.html',
                  controller  : 'contactUsController'
              });
      
      });
      

      在添加最后一个“contactUs”部分时,我注意到我在giftCards 路由后面有一个分号,这会炸毁所有内容。

      【讨论】:

        猜你喜欢
        • 2016-12-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-01
        • 2012-07-11
        • 2018-04-08
        • 2017-04-20
        • 2018-10-02
        • 2016-09-04
        相关资源
        最近更新 更多