Javascript 如何在控制器之间共享来自rest服务的数据

Javascript 如何在控制器之间共享来自rest服务的数据,javascript,angularjs,Javascript,Angularjs,我的问题在于,我在REST服务中有一些数据,我想获取它们,然后发送给其余的控制器。但我不知道如何正确执行,因为服务$http.get()是异步执行的。为了更好地表示问题,我将向您展示我的一段代码 我创建了负责从REST服务获取数据的控制器 MyApp.controller("AppController", ["$scope", "$http", function($scope,$http) { $http.get("http://localhost:8080/library/book"

我的问题在于,我在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;
};
});
MyApp.service(“BookListModel”,function()){
this.createBookList=函数(bookList、authorList、publisherList){
var i=0,j=0,authorName,publisherName,bookListItems=[];
对于(;i
例如,这是我的一项服务。它使用作者和出版商创建图书列表,需要三个参数,我不知道如何将数据从AppController传递到createBookList函数

使用角度服务

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);
});
}]);
注意:我没有测试代码

使用角度服务

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);
});
}]);
注意:我没有测试代码

使用角度服务

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);
});
}]);
注意:我没有测试代码

使用角度服务

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

注意:我没有测试代码

使用工厂/服务进行REST呼叫(不要在控制器中进行)

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

每当一个控制器得到这个承诺时,他们每次都会得到相同的数据,并且您的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;
});

使用工厂/服务进行REST呼叫(不要在控制器中进行)

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

每当一个控制器得到这个承诺时,他们每次都会得到相同的数据,并且您的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;
});

使用工厂/服务进行REST呼叫(不要在控制器中进行)

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

每当一个控制器得到这个承诺时,他们每次都会得到相同的数据,并且您的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;
});

使用工厂/服务进行REST呼叫(不要在控制器中进行)

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

每当一个控制器得到这个承诺时,他们每次都会得到相同的数据,并且您的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;
});
....

你也可以做角度广播。在“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;
});
....

你也可以做角度广播。在“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;
});
....

你也可以做角度广播。在“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;
});
....


你有什么问题?请详细说明……你有什么问题?请详细说明……你有什么问题?请详细说明……你有什么问题?请详细说明。这可能是我的问题的解决方法,但首先我测试了它。我对它进行了一些编辑,以清除一些错误。让我知道进展如何。我认为这是解决我问题的好办法,但我有一个反对意见$http.get()不能从m REST服务返回对象,可能我必须使用$http.get().success(),但我不知道怎么做。then()函数与success()函数相同。您可以在示例中重命名它,它仍然可以工作。确保您期望的数据在response.books中(可能是response.books.data?)。在控制器中,键入:LibraryService.getLibraryData.success(函数(数据){console.log(数据.bookList)});更正:LibraryService.getLibraryData().success(函数(数据){console.log(数据.bookList)});这可能是我问题的解决方法,但首先我测试了它。我对它进行了一些编辑,以清除一些bug。让我知道进展如何。我认为这是解决我问题的好办法,但我有一个反对意见$http.get()不能从m REST服务返回对象,可能我必须使用$http.get().success(),但我不知道怎么做。then()函数与success()函数相同。您可以在示例中重命名它,它仍然可以工作。确保您期望的数据在response.books中(可能是response.books.data?)