如何在控制器之间共享来自休息服务的数据
How share data from rest service between controllers
我的问题在于REST服务中有一些数据,我想获取它们,然后发送到其他控制器。但我不知道如何正确地执行,因为服务$http.get()是异步执行的。我将向您展示我的代码片段,以便更好地表示问题。
我创建了一个控制器,负责从REST服务获取数据。
MyApp.controller("AppController", ["$scope", "$http", function($scope,$http) {
$http.get("http://localhost:8080/library/book").success(function(data) {
$scope.bookList = data;
});
$http.get("http://localhost:8080/library/author").success(function(data) {
$scope.authorList = data;
});
$http.get("http://localhost:8080/library/publisher").success(function(data) {
$scope.publisherList = data;
});
}]);
BookList、authorList、publisherList是其余控制器的构建材料。
好的,我展示另一段代码。
MyApp.service("BookListModel", function() {
this.createBookList = function(bookList, authorList, publisherList) {
var i = 0, j = 0, authorName, publisherName, bookListItems = [];
for(; i < bookList.length; i++) {
for(; j < authorList.length; j++) {
if(bookList[i].authorId == authorList[j].authorId) {
authorName = authorList[j].name;
}
}
j = 0;
for(; j < publisherList.length; j++) {
if(bookList[i].publisherId == publisherList[j].publisherId) {
publisherName = publisherList[j].name;
}
}
j = 0;
bookListItems.push({'title' : bookList[i].title, 'author' : authorName, 'publisher' : publisherName});
authorName = "", publisherName = "";
}
return bookListItems;
};
});
例如,这是我的一个系列。它与作者和出版商一起创建图书列表,需要三个参数,我不知道如何将数据从AppController传递到createBookList函数。
使用工厂/服务进行REST调用(不要在控制器中进行)。
如果您同时进行所有这些调用,则可以使用$q.all()组合promise并获得一个promise。
每当控制器得到这个承诺时,它们每次都会得到相同的数据,并且您的REST服务只会被调用一次。
您的控制器只需调用LibraryService.getLibraryData()并处理获取数据本身的承诺。
MyApp.factory('LibraryService', function($http, $q){
var service = {};
var libraryDataPromise = $q.defer();
$q.all({
books: $http.get("http://localhost:8080/library/book"),
authors: $http.get("http://localhost:8080/library/author"),
publishers: $http.get("http://localhost:8080/library/publisher"),
}).then(function(response){
libraryDataPromise.resolve({
bookList: response.books,
authorList: response.authors,
publisherList: response.publishers
});
});
service.getLibraryData = function() {
return libraryDataPromise.promise;
};
return service;
});
您还可以进行角度广播。在您的"AppController"中:
....
http.get("http://localhost:8080/library/book").success(function(data) {
$scope.$broadcast('bookList-updated', data);
}
....
然后在你想使用数据的其他控制器中这样做:
....
$scope.$on('booklist-updated', function(event, booklist){
$scope.booklist = booklist;
});
....
使用角度服务
app.service('mySharedService', function ($rootScope) {
this.bookList = [];
this.authorList = [];
this.publisherList = [];
this.setBookList = function (bookList) {
this.bookList = bookList;
};
this.setPublisherList = function (publisherList ) {
this.publisherList = publisherList ;
};
this.setAuthorList = function (authorList ) {
this.authorList = authorList ;
};
this.getBookList = function () {
return this.bookList;
};
this.getPublisherList = function () {
return this.publisherList;
};
this.getAuthorList = function () {
return this.authorList;
};
});
确保将您的服务传递''注入您的控制器
MyApp.controller("AppController", ["$scope", "$http", "mySharedService", function($scope,$http) {
$http.get("http://localhost:8080/library/book").success(function(data) {
$scope.bookList = data;
mySharedService.setBookList(data);
});
$http.get("http://localhost:8080/library/author").success(function(data) {
$scope.authorList = data;
mySharedService.setPublisherList(data);
});
$http.get("http://localhost:8080/library/publisher").success(function(data) {
$scope.publisherList = data;
mySharedService.setAuthorList(data);
});
}]);
注意:我没有测试代码。
相关文章:
- AngularJS-需要在index.html页面中访问来自服务的数据
- 将工厂服务数据发送到控制器,以便在视图中使用
- 控制器切换后匿名功能中的访问服务数据发生变化
- 角度:如何刷新服务数据
- 如何访问回调函数之外的异步angularjs服务数据
- 如何将指令模型绑定到异步服务数据
- 基于KendoUI网格内的服务数据的静态下拉列表值选择
- 使用$timeout等待服务数据解析
- 在使用angularjs刷新页面时,不应删除服务数据
- 在ASP.NET MVC中使用屏蔽图绑定到web服务数据
- Angular:在控制器之间共享异步服务数据
- 如何在Mozilla Firefox中获取web服务数据
- ajax GET无法工作(服务数据无法获取)
- 将服务数据保存到控制器的全局变量中
- 为什么在angular中不重置服务数据?
- 在React.js中的组件之间传递Ajax/服务数据
- Angularjs从http url中的服务数据中筛选错误
- 如何检索web服务数据并将其放置在html表中
- AngularJS服务返回数据,但在编辑时会编辑原始服务数据
- OData服务数据绑定