AngularJS,service&许诺

时间:2014-05-02 10:07:08

标签: javascript angularjs

我正在研究angularJS,我需要一些帮助。

这是我想要做的:

  • 我需要建立一个能够获得的服务。存储我的应用程序属性(通过$ http.get())
  • 我想使用thoses属性(1 popup = 1 directive / controller ..)
  • 有几个弹出窗口
  • 我不希望弹出窗口“看到”其余的调用(myService.myData应该足够了)

我的问题:

  • 我无法让服务在控制器调用之前检索数据
  • 我看到了一些方法来做..但是它需要一些应用程序更改(在app.js中),我不希望弹出式开发人员这样做
  • 由于我不能这样做,我无法在我的服务/控制器中制作干净的东西

我希望客户能够制作自己的弹出窗口,而无需关心服务如何调用应用程序。 我知道我可以通过控制器中的常规回调来实现它。但由于它是在其中编码的客户端角色,我希望这部分尽可能简单。

PS:当然我之前测试过一个更简单的实现,在控制器内部有回调等等..它有效..但我需要像下面一样干净:

代码示例: 服务:

angular.module("myService", []).service("MyService", ['$http', function($http){
    this.propertiesInitialized = false;
        this.availableLanguages = [];
        this.availableResultTypes = [];
        this.availableStates = [];
        this.availableCrawlTypes = [];
        this.dateFormat = "";

        this.getProperties = function(callback)
        {
            $http.get("app/application-properties.json").success(function(JSONProperties) {
                this.availableLanguages = JSONProperties.configurations.crawlerLanguages;
                this.availableResultTypes = JSONProperties.configurations.resultTypes;
                this.availableStates = JSONProperties.configurations.states;
                this.availableCrawlTypes = JSONProperties.configurations.crawlTypes;
                this.dateFormat = JSONProperties.configurations.dateFormat;
                this.propertiesInitialized = true;
            });
        }
    }]);

一个弹出窗口:

angular.module("popup1", ["MyService"])
.controller('Controller1', ['$scope', 'MyService', 
    function($scope, MyService) {

    $scope.languages = MyService.availableLanguages;
    $scope.crawlTypes = MyService.availableLanguagesCrawlTypes;
    $scope.resultTypes = MyService.availableLanguagesResultTypes;

}]);

你有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

使用承诺。

angular.module("myService", []).service("MyService", ['$http', '$q', function($http, $q){
    var deffered = $q.defer();
    var theService = this;

    theService.propertiesInitialized = deffered.promise;
    theService.availableLanguages = [];
    theService.availableResultTypes = [];
    theService.availableStates = [];
    theService.availableCrawlTypes = [];
    theService.dateFormat = "";

    $http.get("app/application-properties.json").success(function(JSONProperties) {
        theService.availableLanguages = JSONProperties.configurations.crawlerLanguages;
        theService.availableResultTypes = JSONProperties.configurations.resultTypes;
        theService.availableStates = JSONProperties.configurations.states;
        theService.availableCrawlTypes = JSONProperties.configurations.crawlTypes;
        theService.dateFormat = JSONProperties.configurations.dateFormat;
        theService.propertiesInitialized.resolve();
    });
}]);

angular.module("popup1")
.controller('Controller1', ['$scope', 'MyService', 
    function($scope, MyService) {

    MyService.propertiesInitialized.then(function(){
        $scope.languages = MyService.availableLanguages;
        $scope.crawlTypes = MyService.availableLanguagesCrawlTypes;
        $scope.resultTypes = MyService.availableLanguagesResultTypes;
    });
}]);