【问题标题】:Angular1 can not load local fileAngular1 无法加载本地文件
【发布时间】:2018-05-08 00:56:38
【问题描述】:

当我在 app 目录中添加 menu.json 时,使用 yeoman 生成器编写 angular。并尝试使用该服务获取 json 文件。出现404错误。

angular.module('mealApp')
  .service('foodFinder', function () {
    // AngularJS will instantiate a singleton by calling "new" on this function
      this.getMenu = function() {
        return $.get('menu.json');
      }

  });

【问题讨论】:

    标签: angularjs frontend


    【解决方案1】:

    如果你把你的menu.json 放在app 文件夹中,我可以知道它的位置与index.html 相同。

    $.get('index.html');怎么样

    默认情况下,yeoman 生成器在您使用其命令(例如:grunt server)启动服务器时,它会自动托管服务器(例如:http://localhost:9000)并自动将您的 index.html 加载到 app 文件夹中。

    如果不能,则表示您正在使用另一种方式启动服务器并丢失了相对路径。

    也许你需要找到你的 menu.json 的相对路径(例如:app/menu.json 或其他东西)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 2016-02-04
      • 2019-05-30
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多