同时使用http请求、承诺、ng-options和工厂或服务
Using http requests, promises, ng-options, and factories or services together
我试图从我们的数据库中检索选项列表,我试图使用angular来做到这一点。我以前从未使用过服务,但我知道,如果我要在页面上的其他控制器中使用对象中的数据,这将是实现我想要的效果的最佳方式。
我遵循了几个教程,并组合了一个工厂,该工厂发出http请求并返回数据。我已经尝试了几种方法,但出于某种原因,什么都没有发生。它好像从来没有运行过工厂功能,我不知道为什么。
工厂:resortModule= angular.module('resortApp',[]);
resortModule.factory('locaService',['$http', function ($http){
var locaService= {};
locaService.locations = {};
var resorts = {};
locaService.getLocations=
function() {
$http.get('/url/url/dest/').success(function (data) {
locaService.locations = data;
});
return locaService.locations;
};
return locaService;
//This is a function I would like to run in addition to the first one so multiple variables would be stored and accessible
/*getResorts:
function(destination) {
$http.get('/url/url/dest/' + destination.id).success(function (data) {
resorts = data;
});
return resorts;
}*/
}]);
resortModule.controller('queryController',['$scope', 'locaService', function($scope, locaService) {
$scope.checkConditional= function (){
if($("#location").val() == ""){
$("#location").css('border','2px solid #EC7C22');
}
};
$scope.selectCheck= function (){
$("#location").css('border','2px solid #ffffff');
$(".conditional-check").hide();
};
$scope.resort;
$scope.locations= locaService.getLocations();
}]);
我只希望返回数据,然后分配给$作用域。视图中ng-options要使用的位置。然后,我希望我的另一个函数在单击时运行,以便由变量resort填充下一个字段。我该怎么做呢?任何帮助都太好了!谢谢!
$http service返回一个承诺,你的函数应该返回这个承诺。基本上你的getLocations函数应该像下面这样
locaService.getLocations=
function() {
return $http.get('/url/url/dest/');
};
然后在你的控制器中,你应该使用以下承诺来检索这些选项:
locaService.getLocations()
.then(
function(locations) // $http returned a successful result
{$scope.locations = locations;}
,function(err){console.log(err)} // incase $http created an error, log the returned error);
在控制器中使用jquery或在控制器中操作dom元素不是一个好的做法,你可以使用ng-style或ng-class直接在视图中应用样式和css类。下面是一个应该如何连接起来的示例:
resortModule= angular.module('resortApp',[]);
resortModule.factory('locaService',['$http', function ($http){
var locaService= {
locations: {}
};
var resorts = {};
locaService.getLocations= function() {
return $http.get('/url/url/dest/');
};
return locaService;
//This is a function I would like to run in addition to the first one so multiple variables would be stored and accessible
/*getResorts:
function(destination) {
$http.get('/url/url/dest/' + destination.id).success(function (data) {
resorts = data;
});
return resorts;
}*/
}]);
resortModule.controller('queryController',['$scope', 'locaService', function($scope, locaService) {
/* Apply these styles in html using ng-style
$scope.checkConditional= function (){
if($("#location").val() == ""){
$("#location").css('border','2px solid #EC7C22');
}
};
$scope.selectCheck= function (){
$("#location").css('border','2px solid #ffffff');
$(".conditional-check").hide();
};
*/
$scope.resort;
locaService.getLocations()
.then(
function(locations) // $http returned a successful result
{$scope.locations = locations;}
,function(err){console.log(err)} // incase $http created an error, log the returned error);
}]);
相关文章:
- Angularjs ng-options selected value of dropdownlist
- Angularjs ng-options 问题仅适用于 Chrome 浏览器
- 如何在ng-options中添加两个索引,并使用Angular.js动态设置值
- 不确定在我的情况下使用 ng-options 的最佳方法
- ng-options from a json with angularjs
- Angular JS ng-options 返回 “?对象:005 ?
- AngularJS中的data-ng-options指令
- angularjs ng-options with formate date delete duplicates
- 角度 1.x ng-options 从值开始的负载下拉列表
- 如何在“ng-options”属性中循环访问对象内的数组
- ng-重复共享相同ng模型的ng-options
- ng-init 未在 ng-options 中设置第一个选择选项
- 将枚举值绑定到 ng-options 角度 js
- Angulajs ng-options from a json with child object
- ng-options中的更多选项
- AngularJS:如何获取 ng-options 的过滤数组
- AngularJs 条件显示 ng-options
- ng-repeat中的ng-options - 无法访问$scope中的选定项目
- 错误: [filter:notarray] ng-options 中的预期数组
- 同时使用http请求、承诺、ng-options和工厂或服务