【问题标题】:Angular js How to pass parameters to $location.path() and use this in $routeProvider for routingAngular js 如何将参数传递给 $location.path() 并在 $routeProvider 中使用它进行路由
【发布时间】:2016-07-27 18:01:19
【问题描述】:

点击按钮我有代码可以导航。


$s.onClick = function (pageObj) {
     $location.path('/Next');
}

这里的参数 pageObj 是包含链接(例如:http://sample..)、标题和其他键值的对象。现在我想将链接、标题作为参数传递给 $location.path 所以我尝试使用


$location.path('/Next/' + pageObj.link + '/' + pageObj.title)

这里的链接,我想在下一页使用的标题参数。 pageObj.link 又是 http,所以完整的路由 url 将无法导航。

这是路由的代码


angularModule.config(function ($routeProvider) {
$routeProvider
    .when('/', {
        templateUrl: '/www/temp/Main.html',
        controller: 'MainController'
    })
    .when('/Next', {

        templateUrl: '/www/temp/Select.html',
        controller: 'SelectController'

    })
});

如何检查参数并使用这些参数导航到下一页。我不希望这些参数显示在 url 中。

【问题讨论】:

    标签: angularjs angularjs-routing


    【解决方案1】:

    由于您不希望参数出现在 URL 中,因此绝对最简单的方法是像这样使用$rootScope

    首页

    $rootScope.routeParams.link = "/my/link/here";
    $rootScope.routerParams.title = "My page title here";
    

    第二页

    $scope.link = $rootScope.routerParams.link;
    $scope.title = $rootScope.routerParams.title;
    

    注意

    您需要在app.config 中定义$rootScope.routeParams 对象,类似于:

    app.config(function($rootScope) {
        $rootScope.routeParams = {
            link: null,
            title: null
        }
    });
    

    您还需要将$rootScope 注入到您引用它的每个控制器中,类似于:

    app.controller('FirstPage_Ctrl', function($scope, $rootScope) {
        $rootScope.routeParams.link = "/my/link/here";
        $rootScope.routerParams.title = "My page title here";
    });
    

    【讨论】:

    • 我不想使用 $rootScope。我的要求是通过 $location 发送参数
    猜你喜欢
    • 2015-07-12
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多