我最近爲保存和加載儀表板小部件的位置做了一個指令。這是小部件div的一個簡單屬性。
appControl.directive('saveLoadGridsterPosition', ['whateverStoringDependency', function($whateverStoringDependency)
{
return function(scope, element, attrs) {
var objectLoaded = whateverStoringDependency.load(attrs.saveLoadGridsterPosition);
// Empty or errorous load
if ((objectLoaded == undefined) || (objectLoaded == null)) {
console.log('Error loading ' + attrs.saveLoadGridsterPosition+'\'s position.');
}
else {
// Here you set the row/col somewhere in the scope, for me its like scope.widget.row
scope.widget.row = objectLoaded.row;
scope.widget.col = objectLoaded.col;
}
var save = function() {
// Here you should get the row/col from the scope
var r = scope.widget.row;
var c = scope.widget.col;
var savingObject = {row: r, col: c};
whateverStoringDependency.save(attrs.saveLoadGridsterPosition, savingObject);
}
scope.$on('$destroy', save);
}
}]);
'whateverStoringDependency'
可以是從本地存儲,Cookie或會話保存/負載的東西的定製服務。然後我這樣使用它:
<div gridster="gridsterOpts">
<ul>
<li ng-repeat="widget in widgets" gridster-item row="widget.row" col="widget.col" size-x="widget.sizeX" size-y="widget.sizeY">
<div data-widget="widget" save-load-gridster-position="{{widget.name}}"></div>
</li>
</ul>
</div>
也許這不是很好,但對我來說它完美的作品。它也可以在任何角度事件上做到這一點,就像你定義"savePositions"
並聽取它而不是"$destroy"
。希望能幫助到你!
我需要獲取我添加的widget來獲取'widgetStatus'的id?如果是這樣,我該怎麼做?也FYI:我正在使用jquery-gridster。 – user2942566
這聽起來像你說的,可能有不同的小部件,所以你需要代碼來檢測哪些存在?也許那麼'widgetStatus'應該是逗號描述的ID字符串而不是簡單的布爾值?你可以將字符串解析成數組並通過PHP或JS進行檢查。 – Ecksley