【问题标题】:Using locale in the $resource URL在 $resource URL 中使用语言环境
【发布时间】:2014-12-11 19:14:08
【问题描述】:

我的 AngularJS 应用程序正在从 Rails JSON API 获取服务器数据,并且 API 路由已本地化,例如:

/:locale/api/categories/

我正在尝试定义一个$resource 来启用本地化。到目前为止,我一直没有成功(请参阅下面我尝试过的方法)。


第一次尝试

$resource('/:localeId/api/categories/:categoryId', {
    localeId: $locale.id,
    categoryId: "@id",
    format: 'json'
})

这不起作用。 localeId 仅在创建 $resource 时评估一次。

第二次尝试

$resource('/:localeId/api/categories/:categoryId', {
    localeId: "$locale.id",
    categoryId: "@id",
    format: 'json'
})

这也不行,生成的网址是/$locale.id/api/categories/

第三次尝试

$resource('/:localeId/api/categories/:categoryId', {
    localeId: "@localeId",
    categoryId: "@id",
    format: 'json'
})

这可行,但我每次使用 $resource 时都必须定义 localeId


有没有更好的方法在我的$resource URL 中自动使用$locale.id

【问题讨论】:

    标签: angularjs locale ngresource


    【解决方案1】:

    只需提供一个返回值的函数 (see documentation):

    $resource('/:localeId/...', {
        localeId: function () {
            return $locale.id;
        },
        // ...
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多