2 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果我理解正確的話,你想設(shè)置this.options['enableEmptyCellDrag']
為 的值@Input() editing
。
你想讓 gridster2(我必須承認(rèn)我不知道這是什么)認(rèn)識(shí)到這個(gè)變化。
所以你有兩個(gè)問(wèn)題:
當(dāng)您處于 時(shí)
ngOnChanges
,@Input()
直接訪問(wèn)您將為您提供“舊”值。通常,為了讓 Angular 檢測(cè)對(duì)象的變化,您需要更改對(duì)象的引用。
這就是你ngOnChanges
應(yīng)該的樣子。
ngOnChanges(changes: SimpleChanges) {
if (changes.editing && changes.editing.currentValue) {
// Solve problem 1)
const newValueOfEditingInput = changes.editing.currentValue;
// Solve Problem 2) - Create a new reference for this.options, so that angular (grister2) can detect the change
this.options = {
...this.options,
enableEmptyCellDrag: newValueOfEditingInput
};
}
}
當(dāng)然我還沒(méi)有測(cè)試過(guò),但希望對(duì)你有幫助

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊
恕我直言,我找到了一個(gè)更優(yōu)雅的解決方案。
GridsterConfig 對(duì)象有一個(gè) api.optionsChanged 子對(duì)象,它是一個(gè)函數(shù)。如果你運(yùn)行它,它也會(huì)告訴 Gridster 選項(xiàng)發(fā)生變化,而不必本質(zhì)上重新實(shí)例化對(duì)象(無(wú)論如何,它可能只是運(yùn)行這個(gè)函數(shù))。看起來(lái)更安全、更優(yōu)雅。
因此,您的更改現(xiàn)在可以如下所示:
ngOnChanges(changes: SimpleChanges) {
if (changes.editing && changes.editing.currentValue) {
this.options.enableEmptyCellDrag = changes.editing.currentValue;
this.options.api.optionsChanged();
}
}
我還建議創(chuàng)建一個(gè)如下所示的類,這將防止您被迫檢查這些選項(xiàng)是否存在(if 語(yǔ)句只是檢查是否定義了 GridsterConfig 接口可選選項(xiàng)...所以如果您定義了它們提前沒(méi)有必要這樣做...不知道為什么 Gridster 使存在可選...恕我直言,選項(xiàng)應(yīng)該始終存在,但可以設(shè)置為 null 或默認(rèn)值)。
export class DashboardOptions implements GridsterConfig{
gridType = GridType.Fit;
compactType = CompactType.None;
margin = 10;
outerMargin = false;
outerMarginTop = null;
outerMarginRight = null;
outerMarginBottom = null;
outerMarginLeft = null;
useTransformPositioning = true;
mobileBreakpoint = 720;
minCols = 1;
maxCols = 100;
minRows = 1;
maxRows = 100;
maxItemCols = 100;
minItemCols = 1;
maxItemRows = 100;
minItemRows = 1;
maxItemArea = 2500;
minItemArea = 1;
defaultItemCols = 1;
defaultItemRows = 1;
fixedColWidth = 105;
fixedRowHeight = 105;
keepFixedHeightInMobile = false;
keepFixedWidthInMobile = false;
scrollSensitivity = 10;
scrollSpeed = 20;
enableEmptyCellClick = false;
enableEmptyCellContextMenu = false;
enableEmptyCellDrop = false;
enableEmptyCellDrag = false;
enableOccupiedCellDrop = false;
emptyCellDragMaxCols = 50;
emptyCellDragMaxRows = 50;
ignoreMarginInRow = false;
public draggable = {
enabled: false,
delayStart: 200,
start: () => {},
stop: () => {}
};
public resizable = {
enabled: true,
delayStart: 200,
start: () => {},
stop: () => {}
};
swap = false;
pushItems = true;
disablePushOnDrag = false;
disablePushOnResize = false;
pushDirections = {north: true, east: true, south: true, west: true};
pushResizeItems = false;
displayGrid = DisplayGrid.Always;
disableWindowResize = false;
disableWarnings = false;
scrollToNewItems = false;
api = {
resize: () => {},
optionsChanged: () => {},
};
itemChangeCallback = (item: GridsterItem, itemComponent: GridsterItemComponentInterface) => {};
}
然后,您的更改現(xiàn)在可以如下所示:
ngOnChanges(changes: SimpleChanges) {
this.options.enableEmptyCellDrag = changes.editing.currentValue;
this.options.api.optionsChanged();
}
添加回答
舉報(bào)