1

I have modal with a list of items that can be selected using a checkbox. Now what I want to do is to be able to also deselect an element, such that when I deselect that element it gets removed from the array and the checkbox gets deselected. At the moment my function adds an element, regardless if I select or deselect an item.

This is my html:

<div class="md-list-item-text">
    <md-checkbox ng-model="Item.isChecked " aria-label="Checkbox 1" ng-change="selectItem(modelItem)">
        <h3>{{ $eval('modelItem.'+propertyName) }}</h3>
        <p>{{ $eval('modelItem.'+propertyDesc) }}</p>
    </md-checkbox>
</div>

and this is the function that selects all elements when they are checked.

$scope.selectItem = function (Item) {
    $scope.temp.push(Item.name)
    prepareDisplayText();
}

Does anybody have some ideas, or suggestions on how to rewrite the function to also work for deselected elements?

Thanks in advance!

1 Answer 1

1

Try this-

<div class="md-list-item-text">
    <md-checkbox ng-model="modelItem.isChecked " aria-label="Checkbox 1" ng-change="selectItem(modelItem)">
        <h3>{{ $eval('modelItem.'+propertyName) }}</h3>
        <p>{{ $eval('modelItem.'+propertyDesc) }}</p>
    </md-checkbox>
</div>

$scope.selectItem = function (Item) {
    if(Item.isChecked){
        $scope.temp.push(Item.name)
    }
    else if($scope.temp.indexOf(Item.name) != -1){
        var index = $scope.temp.indexOf(Item.name);
        $scope.temp.splice(index,1);
    }        
    prepareDisplayText();
}

It is working fine for me.

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

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.