加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS路由在PhoneGap中无法正常工作

发布时间:2020-12-17 10:17:56 所属栏目:安全 来源:网络整理
导读:我一直在撞墙,试图找出为什么angularJS路由不能在我的phonegap中工作.我已正确设置所有文件,我没有收到任何错误.我正在尝试直接从angular使用$location.url服务更改网址.因此,当您点击div时,控制器将具有$location.url(“profile”),例如,什么都不会发生.我
我一直在撞墙,试图找出为什么angularJS路由不能在我的phonegap中工作.我已正确设置所有文件,我没有收到任何错误.我正在尝试直接从angular使用$location.url服务更改网址.因此,当您点击div时,控制器将具有$location.url(“profile”),例如,什么都不会发生.我尝试了在 stackoverflow中找到的解决方案,但这对我不起作用.我做错了什么,或者有更好的方法来接近这个?以下是我设置的路由
var app = angular.module("App",["hmTouchevents"])
.config(function($routeProvider) {

$routeProvider
    .when("/index.html",{
        templateUrl: "/views/login.html",controller: "loginCtlr"
    })
    .when("/landing",{
        templateUrl: "/views/landing.html",controller: "landingCtlr"
    })
    .when("/single-view/:id",{
        templateUrl: "/views/single-view.html",controller: "singleViewCtlr"
    })
    .when("/restaurant",{
        templateUrl: "/views/restaurant-info.html",controller: "restaurantCtlr"
    })
    .when("/profile/:id",{
        templateUrl: "/views/profile.html",controller: "profileCtlr"
    })
    .when("/lists/:list",{
        templateUrl: "/views/lists.html",controller: "listsCtlr"
    })
    .when("/follow/:type",{
        templateUrl: "/views/follow.html",controller: "followCtlr"
    });

});

样本控制器将是:

app.controller("listsCtlr",["$scope","$location",function($scope,$location){

$scope.goTo("profile");

}]);

一如往常,任何帮助都非常感谢.

我今晚遇到了类似的问题.这里的核心问题是PhoneGap没有内置的Web服务器.如果您在本地开发,您可能正在使用Web服务器,因此您可以执行以下操作:http:// localhost /#/ profile和路由将起作用.

但是,PhoneGap使用file:// URL加载代码,而不是http://.如果您执行$location.url(“profile”),则只需使用file:// profile替换整个URL,该文件不存在.如果你使用这样的链接,同样的事情:< a href =“/#/ profile”>我的个人资料< / a>

解决方案是以某种方式让您的链接指向正确的位置.如果您使用的是$location.url(),则可以在其前面添加文件路径:$location.url(window.location“#/ profile”)

如果您只是建立一个链接,您应该能够取消前导斜杠以使其成为相对URL:< a href =“/#/ profile”>我的个人资料< / a>变为< a href =“#/ profile”>我的个人资料< / a>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读