Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  130] [ 4]  / answers: 1 / hits: 65544  / 11 Years ago, wed, january 22, 2014, 12:00:00

I have a variable selectedSubTopicId and I have an array of subTopic objects: objectiveDetail.subTopics[]. Each subTopic object has a field subTopicId



I would like to use this to enable or disable and Add topic button. Can I use lodash in the ng-disabled to test this array and report true if any subTopic object element of the array has a subTopicId that is equal to the selectedSubTopicId.



Here's a sample of the data that's in objectiveDetail. In this case there's just one element in the subTopics array.



{objectiveDetailId:285,
objectiveId:29,
number:1,
text:x,
subTopics:[{subTopicId:1,
number:1}]
}


Here is the code in my Angular Controller suggested by thefourtheye:



    $scope.checkDuplicateSubTopicId = function (objectiveDetail, sSubTopic) {
if (_.some(objectiveDetail.subTopics, function(currentTopic) {
return _.contains(currentTopic, selectedSubTopicId);
})) {
return true;
} else {
return false;
}
}


My button with the click function not shown looks like this:



   <button data-ng-disabled=checkDuplicateSubTopicId(objectiveDetail, subTopicId)>
Add Topic
</button>


The problem is that it's not quite working and the button does not show disabled.


More From » lodash

 Answers
14

You didn't ask for how to do it, but I assume that's what you wanted to know.



As I already mentioned, you can use _.some, which will iterate over every element in the array and execute a callback. In that callback you can test whether the value of the topic's property equals the value of the variable:



var result = _.some(objectiveDetail.subTopics, function (topic) {
return topic.subTopicId === selectedSubTopicId;
});


_.some will skip the remaining elements if it found one for which the callback returned true.


[#73019] Tuesday, January 21, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kentrelle

Total Points: 333
Total Questions: 93
Total Answers: 95

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
;