AngularJS:js-factory service在调用之间的流程
AngularJS : js-factory service's flow between the calls
我是 angularjs 的新手,所以我最初通过我在网上找到的基本示例只是为了理解使用的工作和概念。当我遇到"工厂服务创建"的概念(这是一种将数据从服务器公开给 angularjs 中的视图的方法)时,我发现很难理解服务的函数参数和对它的调用之间的流程。
`<html ng-app="countryApp">
<head>
<meta charset="utf-8">
<title>Angular.js Example</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.10/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.10/angular-route.min.js"></script>
<script>
var countryApp = angular.module('countryApp', ['ngRoute']);
countryApp.config(function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'country-list.html',
controller: 'CountryListCtrl'
}).
when('/:countryName', {
templateUrl: 'country-detail.html',
controller: 'CountryDetailCtrl'
}).
otherwise({
redirectTo: '/'
});
});
countryApp.factory('countries', function($http){
return {
list: function(callback){
$http.get('countries.json').success(callback);
},
find: function(name, callback){
$http.get('countries.json').success(function(data) {
var country = data.filter(function(entry){
return entry.name === name;
})[0];
callback(country);
});
}
};
});
countryApp.controller('CountryListCtrl', function ($scope, countries){
countries.list(function(countries) {
$scope.countries = countries;
});
});
countryApp.controller('CountryDetailCtrl', function ($scope, $routeParams, countries){
countries.find($routeParams.countryName, function(country) {
$scope.country = country;
});
});
</script>
</head>
<body>
<div ng-view></div>
</body>
</html>`
所以在我发布的代码中,任何人都可以让我知道或解释"工厂列表和查找方法(特别记住回调参数)"之间的流程吗? 我无法理解为什么同一个工厂方法被自己再次调用(回调参数)
请帮帮我..
关于列表函数
实例化CountryListCtrl
控制器时,countries
服务(即对象)将作为参数传递。
然后调用countries.list
函数(显然countries
服务中定义)并传递回调函数。countries.list
函数发出 GET 请求,如果成功(即成功解析$http承诺),则调用在CountryListController
控制器中调用函数时传入的匿名回调函数,并且$http服务将返回的数据作为参数传递 - 然后匿名函数将其分配给 $scope.countries
属性。
countries.find
函数是相同的基本模式,不同之处在于$routeParams
从路由中获取/:countryName
,并将其作为参数传递到countries.find
函数中,目的是(似乎)从服务器返回的响应数据中挑选出特定国家/地区,然后将其分配给 $scope.country
属性。
我正在评论的代码部分是
countryApp.factory('countries', function($http){
return {
list: function(callback){
$http.get('countries.json').success(callback);
},
find: function(name, callback){
$http.get('countries.json').success(function(data) {
var country = data.filter(function(entry){
return entry.name === name;
})[0];
callback(country);
});
}
};
});
在这里,工厂返回一个具有两个函数的对象,即列表和查找。
这两个函数都有一个名为回调的参数。 回调基本上是服务成功执行时要调用的函数。由于列表和查找都将对服务器进行异步调用,因此您希望在调用完成时收到通知。
然而,Angular有一种更简洁的方式来做到这一点,称为promise。 如果我们实现承诺 API,代码就变成了
countryApp.factory('countries', function($http, $q){
return {
list: function(){
var defered = $q.defer();
$http.get('countries.json').success(function(result){
defered.resolve(result);
})
.error(function(error){
defered.reject(error)
})
return defer.promise
},
find: function(name){
var defered = $q.defer();
$http.get('countries.json').success(function(data) {
var country = data.filter(function(entry){
return entry.name === name;
})[0];
defered.resolve(country);
})
.error(function(error){
defered.reject(error)
})
return defer.promise;
}
};
});
Angulars promise api 在这里有很好的记录
https://docs.angularjs.org/api/ng/service/$q
简而言之,它所说的是承诺对象是一个合约,当异步工作完成时,它将被解析()(成功完成)或被拒绝(未成功完成)并且承诺对象然后函数将被调用。
then(success(), error())
您的控制器将成为。
countryApp.controller('CountryListCtrl', function ($scope, countries){
countries.list().then(function(countries) {
$scope.countries = countries;
});
}, function(error){
console.log("unable to fetch the list of countries : " + error)
});
countryApp.controller('CountryDetailCtrl', function ($scope, $routeParams, countries){
countries.find($routeParams.countryName).then(function(country) {
$scope.country = country;
}, function(error){
console.log("unable to find the country: " + error)
}));
希望它对你有帮助。
我们为AngularJS中的任何应用程序定义模块。然后我们为模块定义配置,在 [] 中我们保留所有必需的依赖项.我们可以定义我们自己的 Angular 指令,该指令将连接 Java 控制器以获取相应格式的值,如 JSON 等。然后,在定义角度控制器时,我们可以在角度控制器中调用我们定义的指令以使数据可用,并且从角度控制器中,我们可以获取角度视图的值,该值将显示在html或任何视图页面中。
- 在Jquery调用之间添加其他函数
- new Datamap() 在调用之间保持内存的共享状态
- 在使用JS的Ajax调用中,ajaxComplete和beforesend之间有什么区别
- 什么's使用脚本标记调用文件和ajax之间的区别
- 调用函数和创建函数实例之间的Javascript差异
- AJAX 调用和表单提交之间的延迟
- 调用焦点时,按钮和跨度之间的区别是什么
- 异步函数调用是否可以在两个同步语句之间完成
- jQuery递归函数调用和Javascript之间有区别吗;s setInterval
- 在使用ajax调用的函数之间调用相同的函数
- 此函数调用之间有什么区别
- AngularJS:js-factory service在调用之间的流程
- (html) Google 地图搜索结果与 Maps/地点 API 调用之间的差异(结果较少)
- 对字符串对象调用 .localeCompare 与构造特制的 Intl.Collator 对象之间的性能差异
- 直接调用方法和使用“.call”方法之间的区别?WSS
- 在视图模型之间调用(KnockOutJS)
- 在行/单元格之间调用表的Onmouseout函数
- 如何在两个函数之间调用变量
- 如何在Javascript中不同对象之间调用方法
- 在脚本标记块之间调用JavaScript函数