启动Ionic应用程序路由

时间:2016-06-20 16:25:24

标签: ionic-framework cordova-plugins deep-linking

找到此解决方案,通过插件

启动应用程序
.run(['$state', '$window',
function($state, $window) {
    $window.addEventListener('LaunchUrl', function(event) {
        // gets page name from url
        var page =/.*:[/]{2}([^?]*)[?]?(.*)/.exec(event.detail.url)[1];
        // redirects to page specified in url
        $state.go('tab.'+ page, {});
    });
}]);

function handleOpenURL(url) {
setTimeout( function() {
    var event = new CustomEvent('LaunchUrl', {detail: {'url': url}});
    window.dispatchEvent(event);
}, 0);}

如果只使用州名,例如,此解决方案效果很好。的myapp://帖子

.state('app.posts', {
url: "/posts",
views: {
  'menuContent': {
    templateUrl: "templates/posts.html",
    controller: 'PostsCtrl'
  }
}})

打开app / posts。

但我需要这样的开放状态

.state('app.post', {
url: "/posts/:postId",
views: {
  'menuContent': {
    templateUrl: "templates/post.html",
    controller: 'PostCtrl'
  }
}})

链接myapp:// post / 2525。 有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:0)

解决了我自己)))

.run(['$state', '$window',
function($state, $window) {
    $window.addEventListener('LaunchUrl', function(event) {
        // gets page name from url
        var page =/.*:[/]{2}([^?]*)[?]?[/]{1}([^?]*)[?]?/.exec(event.detail.url)[1];
        var id =/.*:[/]{2}([^?]*)[?]?[/]{1}([^?]*)[?]?/.exec(event.detail.url)[2];
        // redirects to page specified in url
        // alert ("id:" +id);
        $state.go('app.'+ page, {'postId': + id});
    });
}]);