AngularJS在数组中找不到值
AngularJS not finding values in array
我开发了一个AngularJS应用程序。
在一个视图中,我有一个项目列表,每个项目都有自己的一组单选按钮。列表和按钮是在运行时通过API调用构建的。
列表和单选按钮都可以是任何长度,例如列表项1可能包含3个单选按钮,而列表项n可能包含2个单选按钮。
当用户选择单选按钮时,我启动一个ng-click方法,该方法将列表ID和单选按钮ID发送到控制器。
该视图如下所示,我已经对此进行了测试,并且将值发送到控制器。
<label class="radio-button" ng-repeat="option in checkItemDescription.options">
<input type="radio" name="option_question_id_{{checkItemDescription.fleetcheckitemid}}"
ng-model="checkItemDescription.selected_id"
ng-click="doSomething(checkItemDescription.fleetcheckitemid, option.fleetcheckid)"
ng-value="option.fleetcheckid">
<div class="radio-button__checkmark"></div>
Description: {{option.checkvaluedesc}}
</label>
在我的doSomething()方法中,我试图查看checkItemDescription.flaetceckitemid或选项.freetcheckidid是否在现有数组中。
我使用indexOf()方法来比较值,但该方法似乎不起作用——尽管console.log()显示值已发送到控制器。
下面是我的doSomething()方法。
$scope.doSomething = function(fleetCheckItemID, fleetCheckID)
{
if ( $scope.initialFleetCheckIssueItemIDArray.indexOf(fleetCheckItemID))
{
console.log("Yeah!"); // Never gets to here even though logs show values exist
}
else
{
console.log("Ah!"); // Always get here
}
}
我需要做的是创建一个JSON对象,其中包含列表项ID、单选按钮ID和其他一些值,例如基于用户选择的日期/时间,以发送到我的数据库。
这是最好的方法还是有更好的方法?
indexOf()方法返回在数组中可以找到给定元素的第一个索引,如果不存在,则返回-1。
-引用自MDN。
因此,如果你的元素在集合中是第一个,那么indexOf将返回0,这将导致else块的执行,所以像这样更改你的if条件。
if ( $scope.initialFleetCheckIssueItemIDArray.indexOf(fleetCheckItemID) != -1 )
{
//element found, do stuff here.
}
else
{
//element does not found, else stuff here.
}
相关文章:
- 如何在不使用ajax的情况下将pair值添加到数组并发送到django中的视图
- AngularJS在数组中找不到值
- 根据某些条件验证用户输入,而不是将输入输入到数组中,需要最基本的解决方案
- 正在反序列化jQuery.Serialize的结果(到数组,而不是到窗体)
- 将不存在的项目推送到数组
- 使用Backbone.js,如何绑定到数组's更改不需要手动触发
- Mongoose创建一个文档,如果找不到指定的字段,则更新文档中的数组
- 如何在不重复非数组的情况下将元素推送到数组中
- 为什么下面的“in”运算符找不到这个数组中的字符串
- 在javascript中,如何将相同的对象两次推送到一个数组中,并进行一些修改而不覆盖数组
- 函数看不到数组中的更改
- push 方法将元素添加到数组中不起作用,同时获取具有相同类的元素的 ID 和值
- 为什么将 Mongo Id 推送到数组而不是对象
- 在 JavaScript 对象原型中找不到数组长度属性
- 找不到适用于 Highcharts 的 JSON 数组语法
- jQuery Array 在我的数组中找不到对象
- 为什么在indexOf为的数组中找不到对象
- 对象在数组中,但indexOf不在数组中;找不到编辑
- 找不到 Ruby 数组的 lodash.pull 函数
- jQuery inArray()不工作?它找不到我确信在数组中的字符串