在Angular Material中使用cdkDragConstrainPosition来限制拖放元素的位置时,可能会发现限制并没有作用于组件范围内。这是由于cdkDragConstrainPosition选项是全局级别的,因此需要给它传递一个局部变量来限制范围。
解决方法包括以下步骤:
@Component({
selector: 'app-my-component',
template: `
Drag me!
`
})
export class MyComponent {
myConstraints = { left: 50, top: 50, bottom: 500, right: 500 };
}
这样,在组件范围内使用cdkDragConstrainPosition选项就会受到限制,并且不会影响其他组件的拖放限制。