我開始使用角一週前,我一直在敲我試圖解決這個問題。我有一個包裝$http
的服務,因爲多個控制器調用相同的URL。有一個頁面在帖子上的服務器上有很多業務邏輯,所以我想打電話給$route.reload()
。它似乎工作,因爲在vm.saveItem()
控制器重新初始化後,但$http.get()
從未將信號發送到服務器的新數據。我究竟做錯了什麼?
myModule.factory("itemRepository", function ($http) {
var baseUrl = "/api/inventory";
return {
getLocations: function (itemName) {
return $http({
url: baseUrl + "/" + itemName + "/locators",
method: "GET",
cache: false
});
},
addNewLocator: function (itemName, newLocator) {
return $http.post(baseUrl + "/" + itemName + "/locators", newLocator);
}
};
});
// itemEditorController.js
(function() {
"use strict";
angular.module("app-inventoryitems")
.controller("itemEditorController", itemEditorController);
function itemEditorController($routeParams, $route, itemRepository) {
// initialize vars
var vm = this;
vm.itemName = $routeParams.itemName;
vm.errorMessage = "";
// Models
vm.items = [];
vm.isBusy = true;
vm.newLocator = {};
vm.newLocator.name = vm.itemName;
// PROBLEM HERE, does not call server after post new data
// and $route.reload() was called
itemRepository
.getLocations(vm.itemName)
.then(function (response) {
angular.copy(response.data, vm.items);
}, function (err) {
vm.errorMessage = "Failed to load batches.";
})
.finally(function() {
vm.isBusy = false;
});
vm.saveItem = function() {
vm.isBusy = true;
itemRepository.addNewLocator(vm.itemName, vm.newLocator)
.then(function (response) {
vm.newLocator = {};
$route.reload();
}, function (error) {
vm.errorMessage = "Failed to save new item.";
})
.finally(function() {
vm.isBusy = false;
})
}
}
})();
'false'對於'cache'不是一個有效的值,'GET'請求被瀏覽器緩存。 – zeroflagL
你在哪裏檢查,該請求沒有發送? –
所以是默認的假?它似乎你可以將其設置爲true(根據這篇文章http://stackoverflow.com/questions/14117653/how-to-cache-an-http-get-service-in-angularjs) –