使用AngularJS服务重新加载JSON源
我是AngularJS的新手,我的第一个项目需要你的帮助 我想从JSON加载数据,并使用ng repeat将其显示在表中,这实际上非常有效。但是,我希望允许用户通过重新加载JSON来重新加载表中的数据 所以首先我加载页面。我得到了JSON,我的表被填满了。然后我编辑JSON文件并保存它。当我点击重新加载按钮时,我收到的数据与我加载页面时得到的数据完全相同。我以为是缓存,所以我清理了浏览器缓存并在get请求中设置cache:false,但它不起作用 以下是我的代码:使用AngularJS服务重新加载JSON源,angularjs,json,reload,Angularjs,Json,Reload,我是AngularJS的新手,我的第一个项目需要你的帮助 我想从JSON加载数据,并使用ng repeat将其显示在表中,这实际上非常有效。但是,我希望允许用户通过重新加载JSON来重新加载表中的数据 所以首先我加载页面。我得到了JSON,我的表被填满了。然后我编辑JSON文件并保存它。当我点击重新加载按钮时,我收到的数据与我加载页面时得到的数据完全相同。我以为是缓存,所以我清理了浏览器缓存并在get请求中设置cache:false,但它不起作用 以下是我的代码: var routeApp =
var routeApp = angular.module("app", ["ui.bootstrap", "ngRoute", "ngSanitize", "ngAnimate", "routeAppControllers"]);
routeApp.config(function ($routeProvider) {
$routeProvider
.when("/home", {
templateUrl: "/SCAP/scap/resources/templates/home.html",
controller: "ctrlHome"
})
.when("/dgroups", {
templateUrl: "/SCAP/scap/resources/templates/device-groups.html",
controller: 'ctrlDeviceGroups'
})
.when("/templates", {
templateUrl: "/SCAP/scap/resources/templates/templates.html",
controller: 'ctrlTemplates'
})
.otherwise({
redirectTo: '/home'
});
});
var routeAppControllers = angular.module('routeAppControllers', []);
routeAppControllers.controller('ctrlDeviceGroups', function ($scope, groupsService) {
$scope.dgroups = [];
var promise = groupsService.getGroups();
promise.then(function(data) {
$scope.dgroups = data;
});
$scope.reloadJSON = function() {
$scope.dgroups = [];
console.log("Cleaning all data...");
console.log("Reloading JSON");
var promise = groupsService.getGroups();
promise.then(function(data) {
$scope.dgroups = data;
console.log("New JSON loaded");
console.log(data);
});
}
}).service('groupsService', function($http, $q, $sce) {
var json_file = "json.json";
// Add to use that because it didn't trust the domain...
var trustedUrl = $sce.trustAsResourceUrl(json_file);
var deferred = $q.defer();
console.log("Loading JSON...");
$http(
{
method: 'GET',
url: trustedUrl,
cache: false
}).then(function(data) {
deferred.resolve(data.data);
});
this.getGroups = function() {
return deferred.promise;
}
});
以下是HTML(我删除了无用的行):
重新加载整个表
如果您知道如何重新加载JSON和视图,我将非常感激。我还注意到,但这不是本主题的主题,如果我在$scope.dgroups中推送数据,ng repeat不会刷新
提前感谢,将GET请求移动到服务方法中:
app.service('groupsService', function($http, $sce) {
var json_file = "json.json";
// Add to use that because it didn't trust the domain...
var trustedUrl = $sce.trustAsResourceUrl(json_file);
this.getGroups = function() {
console.log("Loading JSON...");
var config = {
method: 'GET',
url: trustedUrl,
cache: false
};
return $http(config)
.then(function(response) {
return response.data;
});
};
});
它现在似乎起作用了!!因此,如果我理解的话,我的服务只提取了一次文件,每次我调用getGroups时都是相同的数据。它们只建造一次。GET请求只错误执行了一次。它需要在
getGroups
方法中完成,以便在每次调用该方法时执行。所以我假设我的服务现在有点无用了?通过将url作为参数传递,我将使用它加载所有JSON。
app.service('groupsService', function($http, $sce) {
var json_file = "json.json";
// Add to use that because it didn't trust the domain...
var trustedUrl = $sce.trustAsResourceUrl(json_file);
this.getGroups = function() {
console.log("Loading JSON...");
var config = {
method: 'GET',
url: trustedUrl,
cache: false
};
return $http(config)
.then(function(response) {
return response.data;
});
};
});