最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

将多个参数从控制器传递到工厂服务,从而进行REST调用和返回(可选)数据

IT培训 admin 4浏览 0评论

将多个参数从控制器传递到工厂服务,从而进行REST调用和返回(可选)数据

我正在尝试创建使用服务(工厂)和ngResource的代码,以向RESTful服务发送请求,该服务执行并执行操作并根据业务逻辑发回可选结果。

我知道一个问题,谈论在Passing multiple parameters from controller to factory in angularjs发现类似的事情。我遇到了让它为我工作的问题,而且还缺乏解释。

我写这篇文章是为了理解它是如何工作的,最佳实践,所以有一个存储库对未来的新手有好处。对上面提供的解决方案的作者没有不尊重,但它可能假设我还没有达到一定程度的聪明;-)

开始:

我的控制器

swof.controller('scheduleController', ['$scope', '$log', '$http', '$filter','moment', 'scheduleServicePeriod', function($scope, $log, $http, $filter, moment, scheduleServicePeriod) {

  $scope.name = 'scheduleController::set default values and provide function to generate new schedule';
  $log.info('Controller: '+ $scope.name);
  $scope.years = ["2017", "2018", "2019","2020","2021","2022"];
  $scope.selectedYear = "2017";
  $scope.selectedPeriod = Math.ceil(moment().format('w')) | 1 ;
  $scope.genSchedule = function()
  {
    // when button is pressed function 'genSchedule' is called that either has default value for selectedYear and selectedPeriod or one user has selected. This bit works
    scheduleServicePeriod.post({ s_year: $scope.selectedYear, s_period: $scope.selectedPeriod }).$promise.then(function(data){
      $scope.schedulegen = data;
        $scope.genScheduleResponse=moment().format('h:mm:ss a') + " " + data.message;
    }, function(data) {
      $log.error();('Error: ' + data);
    })};

}]);
回答如下:

它确实有效!我在做错的是在使用console.log进行调试时打印出错误的值以及我在UI中显示响应消息的方式。

总结一下,我想按POST / api / schedules / period //进行API调用//

该调用进行计算并发回我想在屏幕上显示最终用户的响应。

这是可以改进的工作代码,但对于基本用例来说却是一个相当不错的例子。如果您按照示例进行逻辑推理,则发送参数的语法相当简单。

调节器

swof.controller('scheduleController', ['$scope', '$log', '$http', '$filter','moment', 'scheduleServicePeriod', function($scope, $log, $http, $filter, moment, scheduleServicePeriod) {

  $scope.name = 'scheduleController::set default values and provide function to generate new schedule';
  $log.info('Controller: '+ $scope.name);
  $scope.years = ["2017", "2018", "2019","2020","2021","2022"];
  $scope.selectedYear = "2017";
  $scope.selectedPeriod = Math.ceil(moment().format('w')) | 1 ;

  $scope.genSchedule = function()
  {
    scheduleServicePeriod.post({ s_year: $scope.selectedYear, s_period: $scope.selectedPeriod }).$promise.then(function(data){
      $scope.schedulegen = data;
      $scope.genScheduleResponse=moment().format('h:mm:ss a') + " " + data.message;
    }, function(data) {
      $log.error();('Error: ' + data);
    })};

}]);

将多个参数从控制器传递到工厂服务,从而进行REST调用和返回(可选)数据

我正在尝试创建使用服务(工厂)和ngResource的代码,以向RESTful服务发送请求,该服务执行并执行操作并根据业务逻辑发回可选结果。

我知道一个问题,谈论在Passing multiple parameters from controller to factory in angularjs发现类似的事情。我遇到了让它为我工作的问题,而且还缺乏解释。

我写这篇文章是为了理解它是如何工作的,最佳实践,所以有一个存储库对未来的新手有好处。对上面提供的解决方案的作者没有不尊重,但它可能假设我还没有达到一定程度的聪明;-)

开始:

我的控制器

swof.controller('scheduleController', ['$scope', '$log', '$http', '$filter','moment', 'scheduleServicePeriod', function($scope, $log, $http, $filter, moment, scheduleServicePeriod) {

  $scope.name = 'scheduleController::set default values and provide function to generate new schedule';
  $log.info('Controller: '+ $scope.name);
  $scope.years = ["2017", "2018", "2019","2020","2021","2022"];
  $scope.selectedYear = "2017";
  $scope.selectedPeriod = Math.ceil(moment().format('w')) | 1 ;
  $scope.genSchedule = function()
  {
    // when button is pressed function 'genSchedule' is called that either has default value for selectedYear and selectedPeriod or one user has selected. This bit works
    scheduleServicePeriod.post({ s_year: $scope.selectedYear, s_period: $scope.selectedPeriod }).$promise.then(function(data){
      $scope.schedulegen = data;
        $scope.genScheduleResponse=moment().format('h:mm:ss a') + " " + data.message;
    }, function(data) {
      $log.error();('Error: ' + data);
    })};

}]);
回答如下:

它确实有效!我在做错的是在使用console.log进行调试时打印出错误的值以及我在UI中显示响应消息的方式。

总结一下,我想按POST / api / schedules / period //进行API调用//

该调用进行计算并发回我想在屏幕上显示最终用户的响应。

这是可以改进的工作代码,但对于基本用例来说却是一个相当不错的例子。如果您按照示例进行逻辑推理,则发送参数的语法相当简单。

调节器

swof.controller('scheduleController', ['$scope', '$log', '$http', '$filter','moment', 'scheduleServicePeriod', function($scope, $log, $http, $filter, moment, scheduleServicePeriod) {

  $scope.name = 'scheduleController::set default values and provide function to generate new schedule';
  $log.info('Controller: '+ $scope.name);
  $scope.years = ["2017", "2018", "2019","2020","2021","2022"];
  $scope.selectedYear = "2017";
  $scope.selectedPeriod = Math.ceil(moment().format('w')) | 1 ;

  $scope.genSchedule = function()
  {
    scheduleServicePeriod.post({ s_year: $scope.selectedYear, s_period: $scope.selectedPeriod }).$promise.then(function(data){
      $scope.schedulegen = data;
      $scope.genScheduleResponse=moment().format('h:mm:ss a') + " " + data.message;
    }, function(data) {
      $log.error();('Error: ' + data);
    })};

}]);
发布评论

评论列表 (0)

  1. 暂无评论