简体   繁体   English

AngularJS不在数组中找到值

[英]AngularJS not finding values in array

I have a AngularJS app developed. 我开发了一个AngularJS应用。

On a view I have a list of items - each with its own set of radio buttons. 在视图上,我有一个项目列表-每个项目都有其自己的单选按钮集。 The list and buttons are built at run-time from an API call. 列表和按钮是在运行时通过API调用构建的。

Both the list and radio buttons can be of any length eg list item 1 might contain 3 radio buttons while list item n might contain 2 radio buttons. 列表和单选按钮都可以是任意长度,例如,列表项1可能包含3个单选按钮,而列表项n可能包含2个单选按钮。

When the user selects a radio button I fire a ng-click method that sends both the list ID as well as the radio button ID to the controller. 当用户选择单选按钮时,我会触发ng-click方法,该方法会将列表ID和单选按钮ID都发送到控制器。

The view looks as follows and I have tested this and the values are being sent to the controller. 该视图如下所示,我已经对此进行了测试,并且值已发送到控制器。

<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>

In my doSomething() method i am trying to see if either the checkItemDescription.fleetcheckitemid or the option.fleetcheckid id is in a existing array. 在我的doSomething()方法中,我试图查看checkItemDescription.fleetcheckitemidoption.fleetcheckid id是否在现有数组中。

I use indexOf() method to compare the values but the method does not seem to work - even though console.log() shows values are sent to the controller. 我使用indexOf()方法比较这些值,但是该方法似乎不起作用-即使console.log()显示将值发送到控制器。

Below is my doSomething() method. 下面是我的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
    }
}

What I need to do is to to create a JSON object with the list item ID, radio button ID and some other values eg date/time based on the user selection to send to my Database. 我需要做的是创建一个JSON对象,其中包含列表项ID,单选按钮ID和其他一些值,例如基于要发送到我的数据库的用户选择的日期/时间。

Is this the best way to do it or is there a better way? 这是最好的方法还是有更好的方法?

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present. indexOf()方法返回可以在数组中找到给定元素的第一个索引;如果不存在,则返回-1。

-referenced from MDN . MDN引用。

so if your element is first in the collection then indexOf would return 0 which would result in the execution of else block so change your if condition like this. 因此,如果您的元素在集合中位于第一个位置,则indexOf将返回0,这将导致else块的执行,因此请更改您的if条件。

if ( $scope.initialFleetCheckIssueItemIDArray.indexOf(fleetCheckItemID) != -1 )
{
    //element found, do stuff here.
}
else
{
    //element does not found, else stuff here.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM