【问题标题】:Codeigninter AngularJs routing with base URL带有基本 URL 的 Codeigniter AngularJs 路由
【发布时间】:2016-10-13 12:49:39
【问题描述】:

我在 angularJS 上进行路由以符合代码点火器配置中设置的基本 url 的挑战

基本网址 = http://localhost:8080/example/

例如,如果我当前的网址是

http://localhost:8080/example/details

并且有来自该页面的 Angular 控制器的 Angular 服务调用,如下所示

$http.get("api/gallery/" + 4).success(function (data) {
        galleryResource.getAll().$promise.then(function (response) {
            console.log(response);
        }, function (response) {
            console.log(response);
        });

调用的 url 将是

http://localhost:8080/example/api/gallery/4

但是当url是现在

http://localhost:8080/example/details/4

同样的服务被调用,将被调用的url将是

http://localhost:8080/example/details/api/gallery/4

我现在的问题是,有没有办法确保 api url 不跟随地址栏中的当前 url 从基本 url 开始,因为在上面的示例中我希望 api url 是

http://localhost:8080/example/api/gallery/4

当我在带有 url 的页面上时

http://localhost:8080/example/details/4 感谢期待您的回复``

【问题讨论】:

    标签: php angularjs codeigniter


    【解决方案1】:

    您必须获取您的基本 url 或将其设置为 AngularJS 中的常量。

    类似 app.constant('base_url', 'http://localhost:8080/example');

    然后在你的 javascript 中,url 从

    转换
    $http.get("api/gallery/" + 4) 
    

    $http.get(base_url + "api/gallery/" + 4)
    

    这里有一个更详细的例子来说明如何定义一个常量: AngularJS constants

    基本上,您需要为您的 API 设置一个基本 url 并在每个请求中使用它。这也很有用,因为如果您更改 API 的 url,也许您将 api 与 api.localhost.com 和 localhost.com 之类的前端分开,您可以将 base_url 更改为 api.localhost.com 一切都会正常工作很好。

    【讨论】:

      猜你喜欢
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多