AngularJS, service & promises
AngularJS, service & promises
我在angularJS工作了一段时间,我需要一些帮助…
我想这样做:
- 我需要建立一个服务,将获得&存储我的应用程序属性(通过$http.get())
- 我想有几个弹出使用这些属性(1弹出= 1指令/控制器..)
- 我不想让弹出窗口"看到"其余的调用(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;
}]);
你有什么想法吗?
使用承诺
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;
});
}]);
相关文章:
- jquery点击函数select&取消选择
- 可以简化嵌套的延迟Q Promises解析吗
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- Angular js-返回一个包含类似
- 一点javascript元编程&可链接的设置器
- 通过JSON&比较时间
- Jquery模板,如果xx&&如果yy
- jQuery工具验证器自定义效果-添加&消除影响
- Twitter引导程序Typeahead-Id&标签
- AngularJS&JSON-从Previous&下一个对象
- 什么是&&在没有if的行中的变量之间
- 关于引入外部javascript文件的问题&css通过https
- 指令的模板必须只有一个根元素:With restrict E&替换true
- 使用ajax的服务器端分页&jQuery
- Javascript,如果条件在没有&&逻辑运算符当&&它不起作用
- 如何使用dropzone&vueJs
- 如何准确执行加载脚本&退出弹出窗口
- 如何添加CSS&JavaScript进入Windows窗体应用程序C#.net
- Solr查询以按日期月份获取数据&年
- 显示&作为&在jsp中使用angularjs而不使用ng-bind-html