0

Is it somehow possible to say if i >= 2 then [cdkDropListConnectedTo]="[one, two]", otherwise [cdkDropListConnectedTo]="[three, four]"?

*ngIf didn't work for me. I also searched for "Angular ngif directives". However, the answers I was looking for did not come out.

<div *ngFor="let task of this.tasks; let i = index">
    <div>
        <div
            cdkDropList
            id="task-container-{{i}}"
            #ref{{i}}="cdkDropList"
            [cdkDropListData]="task.getDropListData()"
            [cdkDropListConnectedTo]= "[]"
            (cdkDropListDropped)="drop($event)">
            <div>
                <p>{{task.getHeadline()}}</p>
            </div>
        </div>
    </div>
</div>
2
  • 1
    If I understand what you try to do, you are assigning the value based on condition. [cdkDropListConnectedTo]="i >= 2 ? [one, two] : [three, four]". *ngIf is used for rendering the element by condition. Commented Dec 10, 2022 at 13:06
  • what is one, two in [one, two] ? Is this array of strings? Commented Dec 10, 2022 at 13:15

2 Answers 2

3

From your question, you are assigning the value based on the condition. You can achieve with:

[cdkDropListConnectedTo]="i >= 2 ? [one, two] : [three, four]"

*ngIf is used to render the element or not by the condition. Hence *ngIf is not suitable in your scenario.

Unless you are attempting with *ngIf with else template as:

<div *ngIf="i > 2; else elseTemplate"
  cdkDropList
  id="task-container-{{i}}"
  #ref{{i}}="cdkDropList"
  [cdkDropListData]="task.getDropListData()"  
  [cdkDropListConnectedTo]="[one, two]"
  (cdkDropListDropped)="drop($event)">
  ...
</div>

<ng-template #elseTemplate
  cdkDropList
  id="task-container-{{i}}"
  #ref{{i}}="cdkDropList"
  [cdkDropListData]="task.getDropListData()"
  [cdkDropListConnectedTo]="[three, four]"
  (cdkDropListDropped)="drop($event)">
  ...
</ng-template>
Sign up to request clarification or add additional context in comments.

2 Comments

I get a parse error when using task-container-{{i}} in the ngIf. [cdkDropListConnectedTo]= "i >= 2 ? [task-container-{{i}}] : [three, four]"
No, that is not the correct way. [cdkDropListConnectedTo]="i >= 2 ? [one, two] : [three, four]"
1

Bind a function like this:

[cdkDropListConnectedTo]= "getDropList(i)"

Now inside your ts file, create the callback:

getDropList (i) {
 return i >= 2 ? [one, two] : [three, four];
}

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.