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 e.g. 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.fleetcheckitemid或option.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 e.g. 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?
这是最好的方式还是有更好的方法?
1 个解决方案
#1
2
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.
}
#1
2
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.
}