AngularJS:js-factory service在调用之间的流程

AngularJS : js-factory service's flow between the calls

本文关键字:之间 调用 js-factory service AngularJS      更新时间:2023-09-26

我是 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或任何视图页面中。