0

Here I am binding values to ng-click as

<input type="checkbox"  ng-model="a.CheckAssign"ng-click="myFunctionnew(a.ENQUIRY_CODE,a.CheckAssign)" />
enquiryArr=[]
$scope.myFunctionnew = function(enqcode,checkassign)
{
enquiryArr.push(enqcode + '&' + checkassign)
var uniqueNames = [];
$.each(enquiryArr, function (i, el) {
    if ($.inArray(el, uniqueNames) === -1) {
        uniqueNames.push(el);
        console.log(uniqueNames)
    }
    else
    {
        console.log(uniqueNames);
    }
}

Here when I check and uncheck the checkbox the value is storing multiple Please help me how can I store Enquiry code and T/F values.

1
  • Can you please provide your output and data? Commented Feb 25, 2019 at 13:56

2 Answers 2

1

If I understand your issue correctly, you're running into duplicate entries in your enquiryArr. You're running into this because you're trying to manually keep track of the array in an odd fashion. While there are ways to correct the way you're storing it, it's even easier to have angular track what is checked and what is not.

Using the following DataSet:

$scope.enquiries = [
  {ENQUIRY_CODE: 'ENQUIRY_1', ENQUIRY_NAME: 'First Enquiry', selected: false},
  {ENQUIRY_CODE: 'ENQUIRY_2', ENQUIRY_NAME: 'Second Enquiry', selected: false},
  {ENQUIRY_CODE: 'ENQUIRY_3', ENQUIRY_NAME: 'Third Enquiry', selected: false},
  {ENQUIRY_CODE: 'ENQUIRY_4', ENQUIRY_NAME: 'Fourth Enquiry', selected: false}
];

And binding the selected field to your checkboxes ng-model

<div ng-repeat="e in enquiries">
  <input type="checkbox" ng-model="e.selected" />
  {{e.ENQUIRY_NAME}}
</div>

You can determine which one is checked simply by looking at the $scope.enquiries array and looking for selected: true (or false if you want to know the ones not checked).

In javascript you can use a simple Array Filter to get the selected values as such:

$scope.enquiries.filter(e => e.selected);

Or if you want to get them in your template, you can also use the filter pipe as such:

{{(enquiries | filter:{selected:true})}}

Here is an interactive plunkr that you can view this.

Misc Note: If you do want to run something when the checkbox is clicked, you can still use an ng-click and run your custom code, however, I would recommend using ng-change as there are other ways to change the selected value of a checkbox other than clicking.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank u soo much for your Efforts
0

I have test your code and its working fine. your problem is not so much clear so you can check following test code.

<div ng-controller="MyController">
<div ng-repeat="dt in data">
 <input type="checkbox"  ng-model="CheckAssign" ng-click="myFunctionnew(dt.name,dt.volume)" />
</div>
</div>

Javascript

$scope.myFunctionnew = function(enqcode,checkassign)
{
enquiryArr.push(enqcode + '&' + checkassign)
var uniqueNames = [];
for(i=0; i <enquiryArr.length; i++) {
    if ($.inArray(enquiryArr[i], uniqueNames) == -1) {
        uniqueNames.push(enquiryArr[i]);
        console.log(uniqueNames)
    }
    else
    {
        console.log(uniqueNames);
    }
}
}

JSFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.