javascript - 如何在 Controller 之间共享来自其余服务的数据

标签 javascript angularjs

我的问题在于我在 REST 服务中有一些数据,我想获取它们然后发送到其余的 Controller 。但我不知道如何正确执行,因为服务 $http.get() 是异步执行的。我将向您展示我的一段代码,以便更好地表示问题。

我创建了 Controller ,负责从 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 是其余 Controller 的构建 Material 。

好的,我展示另一段代码。

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 调用(不要在 Controller 中执行)。

如果您同时进行所有这些调用,则可以使用 $q.all() 组合这些 Promise 并获得单个 Promise。

每当 Controller 获得该 promise 时,他们每次都会获得相同的数据,并且您的 REST 服务只会被调用一次。

您的 Controller 只需调用 LibraryService.getLibraryData() 并处理获取数据本身的 promise 。

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;
});

关于javascript - 如何在 Controller 之间共享来自其余服务的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28864478/

相关文章:

javascript - 交叉口观察者 API : Observe the center of the viewport

javascript - 无法访问 Redis 远程服务器并且无法读取或写入 Redis 数据库

javascript - ng-show 未正确更新

css - 如何在没有 html head 标签的情况下将外部 css 文件添加到 AngularJS

javascript - 使用lazyload的优势?

javascript - 使用 javascript knockout 绑定(bind) View

javascript - 使用 jQuery 显示/隐藏复选框

javascript - 同一条信息重复3次

javascript - 如何动态设置元素的高度?

javascript - 迭代嵌套对象 - 如果字段匹配变量,则更新 $scope