我擁有的是簡單的CRUD操作。項目列在頁面上,當用戶點擊按鈕添加,模式彈出,用戶輸入數據,數據被保存,應自動(不刷新)被添加到頁面上的列表。Angularjs必須刷新頁面才能看到更改
服務:
getAllIncluding: function(controllerAction, including) {
var query = breeze.EntityQuery.from(controllerAction).expand(including);
return manager.executeQuery(query).fail(getFailed);
},
addExerciseAndCategories: function(data, initialValues) {
var addedExercise = manager.createEntity("Exercise", initialValues);
_.forEach(data, function(item) {
manager.createEntity("ExerciseAndCategory", { ExerciseId: addedExercise._backingStore.ExerciseId, CategoryId: item.CategoryId });
});
saveChanges().fail(addFailed);
function addFailed() {
removeItem(items, item);
}
},
控制器:
$scope.getAllExercisesAndCategories = function() {
adminCrudService.getAllIncluding("ExercisesAndCategories", "Exercise,ExerciseCategory")
.then(querySucceeded)
.fail(queryFailed);
};
function querySucceeded(data) {
$scope.queryItems = adminCrudService.querySucceeded(data);
var exerciseIds = _($scope.queryItems).pluck('ExerciseId').uniq().valueOf();
$scope.exerciseAndCategories = [];
var createItem = function (id, exercise) {
return {
ExerciseId: id,
Exercise : exercise,
ExerciseCategories: []
};
};
// cycle through ids
_.forEach(exerciseIds, function (id) {
// get all the queryItems that match
var temp = _.where($scope.queryItems, {
'ExerciseId': id
});
// go to the next if nothing was found.
if (!temp.length) return;
// create a new (clean) item
var newItem = createItem(temp[0].ExerciseId, temp[0].Exercise);
// loop through the queryItems that matched
_.forEach(temp, function (i) {
// if the category has not been added , add it.
if (_.indexOf(newItem.ExerciseCategories, i.ExerciseCategory) < 0) {
newItem.ExerciseCategories.push(i.ExerciseCategory);
}
});
// Add the item to the collection
$scope.items.push(newItem);
});
$scope.$apply();
}
這裏是我從控制器添加新的數據:
所以我的問題是,爲什麼不更新adminCrudService.addExerciseAndCategories($scope.selectedCategories, { Name: $scope.NewName, Description: $scope.NewDesc });
列表實時(當我保存時我必須刷新頁面)。
編輯
這裏是我的querySuceeded
querySucceeded: function (data) {
items = [];
data.results.forEach(function(item) {
items.push(item);
});
return items;
}
EDIT 2
我相信我已經縮小我的問題!
因此PW Kad因爲我試圖幫助我解決這個問題而失去了兩個小時(廣告我非常非常感謝他),但不幸的是沒有成功。我們主要試圖修復我的服務,所以當我回到我的電腦時,我再次嘗試修復它。我相信我的服務很好。 (我在Kad建議的答案中做了一些修改)。 我相信問題是在控制器中,我記錄了$ scope.items,並且當我添加新項目時它們不會更改,之後我記錄了$ scope.queryItems,並且我注意到它們在添加新項目(不刷新)。所以可能的問題將通過$加載初始數據後$ $ scope.queryItems來解決,但目前我不太確定如何執行此操作。
正如擡起頭,那是絕對噸碼你發佈了這些內容,並且仔細研究它是非常耗時的。如果您將其降至您遇到問題的代碼,確定問題發生的位置並尋求特定幫助,您可能會獲得更多成功。 –
將這些代碼放入在線演示中以節省您的時間和時間會更有用。 – zsong
感謝您的建議,我會這樣做 – hyperN