Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

javascript - 如何在AngularJS中使用$ HTTP重新检索数据(How do I retreive data again with $HTTP in AngularJS)

I am using AngularJS $http to get data and creating a list of objects from the data returned.(我正在使用AngularJS $ http来获取数据并根据返回的数据创建对象列表。)

When the page loads $http gets and returns the data successfully.(页面加载时,$ http会成功获取并返回数据。) How do retrieve the data again without refreshing the page?(如何在不刷新页面的情况下再次检索数据?) $http.get("files/data.json") .then(function (response) { // javascript to assign returned data to list of objects }).catch(function (response) { console.log("error"); });   ask by Jigoro Kano translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You also can use a service .(您还可以使用服务 。)

angular .module('yourModule', []) .service('HTTPService', ['$http', function($http) { this.get = () => $http.get('files/data.json'); }]) .controller('yourController', ['HTTPService', function(HTTPService) { HTTPService.get().then(function(response) { // javascript to assign returned data to list of objects }).catch(function (response) { console.log('error'); }); }]); Then, you can inject this service wherever you want to use.(然后,您可以在任何要使用的地方注入该服务。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...