我尝试重新加载数据按钮。继承人我的json:
\Redirect::back();
我的javascript:
[
{
"name": "AAAAAA",
"data": "False"
},
{
"name": "BBBBBB",
"data": "45%"
},
{
"name": "CCCCC",
"data": "12%"
}
]
我的HTML:
app.service('service', function($http, $q) {
var deferred = $q.defer();
$http.get('names.json').then(function(data) {
deferred.resolve(data);
});
this.getNames = function() {
return deferred.promise;
}
});
app.controller('FirstCtrl', function($scope, service, $http) {
var vm = this;
vm.reloadData = function() {
console.log("reloading");
vm.loadData();
};
vm.loadData = function() {
var promise = service.getNames();
promise.then(function(data) {
$scope.names = data.data;
console.log($scope.names);
});
}
vm.loadData();
});
我的数据应该在点击功能后重新加载" vm.reloadData()"但没有任何反应,我的数据不刷新。 提前感谢您的回答。
答案 0 :(得分:1)
试试这个:
添加了不在缓存
中保留请求数据的规定 app.service('service', function($http, $q) {
this.getNames = function() {
return $http.get('names.json', { cache: false});
}
});
app.controller('FirstCtrl', function($scope, service) {
var vm = this;
vm.reloadData = function() {
console.log("reloading");
vm.loadData();
};
vm.loadData = function() {
var promise = service.getNames();
promise.then(function(data) {
$scope.names = data.data;
console.log($scope.names);
});
}
vm.loadData();
});
答案 1 :(得分:0)
根据您的实现,您将在服务中继承承诺,该承诺已经解决。
您应该在deferred
服务方法中定义getNames()
。
app.service('service', function ($http, $q) {
this.getNames = function () {
var deferred = $q.defer();
$http.get('names.json').then(function (data) {
deferred.resolve(data);
});
return deferred.promise;
}
});
答案 2 :(得分:0)
将您的service
代码更改为
this.getNames = function() {
return $http.get('names.json');
}