【问题标题】:cannot use state params variable不能使用状态参数变量
【发布时间】:2016-12-19 10:22:40
【问题描述】:

我正在这样检索 url 中的 id:

  var id=$stateParams.carId;

与

  console.log(id) 

我得到了正确的值:91 问题是:当我尝试将此值与 lodash 一起使用时,我得到“未定义”:

 console.log(id) //91
 var result = _.find(modules, {id: id});
 console.log(result); //undefined

但是通过直接使用值:91,我得到了我正在寻找的对象

console.log(id) //91
var result = _.find(modules, {id: 91});
console.log(result); //it works

在我有的模块中

 console.log(modules)// Array[244]

我要查找的元素在哪里:

manager:John
user:Marc
documentation: Array[0]
id: 91

【问题讨论】:

  • 模块中有什么,你能把它控制台记录出来给我们看吗?
  • 我刚刚用“模块”编辑了帖子

标签: angularjs lodash


【解决方案1】:

请检查 $stateParams.carId 的数据类型。

我想 $stateParams.carId 是 String 类型,因此它无法从模块中找到合适的值。

typeof($stateParams.carId) 

使用上述命令检查数据类型

【讨论】:

  • 我得到了这个:字符串
  • 这意味着它们是不同的数据类型并且无法匹配。您可以尝试其中任何一种 1. 将 $stateParams.carId 的 Datatype 更改为 Number(或) 2. 使用 parseInt 将其转换为 Number _.find(modules, {id: parseInt(id)});
  • 尝试将值解析为整数,如 var result = _.find(modules, {id: parseInt(id)}) 或更好的方法是修复您的数据,使其成为正确的数据类型.
【解决方案2】:

试试这个:

var result = _.find(modules, { id: _.get($stateParams, 'carId') });

另外,我想看看模块中有什么?请问可以console.log()吗?

【讨论】:

  • 我还有“未定义”。我用“模块”编辑了我的帖子
猜你喜欢
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 2014-11-26
  • 1970-01-01
  • 1970-01-01
  • 2015-05-20
  • 1970-01-01
  • 2022-10-02
相关资源
最近更新 更多