0

So i have an array of objects that is being displayed as such:

<div class="panel panel-default" ng-repeat="row in jobprofiles">
<div class="panel-heading">
    {{row.name}}
</div>
<ul class="list-group alt">
    <li class="list-group-item" ng-repeat="competence in row.selectedCompetence">
        <div class="media">
            <span class="pull-left thumb-sm"></span>
            <div class="pull-right text-success m-t-sm">
                <button class="btn btn-danger" ng-click="competence.remove()"><i class="fa fa-times"></i></button>
            </div>
            <div class="media-body">
                <div><a href="">{{competence.name}}</a></div>
                <small class="text-muted">{{competence.competence_type_id == 1 ? 'Faglig' : 'Personlig'}}</small>
            </div>
        </div>
    </li>
</ul>

as you can see that each li has a button where i want to remove the competence object of the inner array.

Can anyone tell me how i might do that?

3 Answers 3

1

Assuming your jobprofiles & selectedCompetence are objects

<div class="panel panel-default" ng-repeat="(indexRow, row) in jobprofiles ">
<div class="panel-heading">
    {{row.name}}
</div>
<ul class="list-group alt">
    <li class="list-group-item" ng-repeat="(indexCompetence, competence) in row.selectedCompetence">
        <div class="media">
            <span class="pull-left thumb-sm"></span>
            <div class="pull-right text-success m-t-sm">
                <button class="btn btn-danger" ng-click="remove(indexRow, indexCompetence)"><i class="fa fa-times"></i></button>
            </div>
            <div class="media-body">
                <div><a href="">{{competence.name}}</a></div>
                <small class="text-muted">{{competence.competence_type_id == 1 ? 'Faglig' : 'Personlig'}}</small>
            </div>
        </div>
    </li>
</ul>

and in controller

$scope.remove(rowIndex, competenceIndex) {
    delete $scope.jobprofiles[rowIndex][competenceIndex];
}

In case if they are Arrays, you can refer to https://docs.angularjs.org/api/ng/directive/ngRepeat

for

track by

expression.

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

Comments

0

Add a remove function on the scope which takes, the competence and the inner array, and removes the item with the splice function:

$scope.removeCompetence(competence, selectedCompetence) {
    var index = selectedCompetence.indexOf(competence);
    selectedCompetence.splice(index, 1);
}

In your HTML:

<button class="btn btn-danger" ng-click="removeCompetence(competence, row.selectedCompetence)"><i class="fa fa-times"></i></button>

Comments

0

What about <button class="btn btn-danger" ng-click="row.selectedCompetence.splice($index, 1)"><i class="fa fa-times"></i></button> ?

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.