【问题标题】:Passing argument(s) to a service in AngularJs将参数传递给 AngularJs 中的服务
【发布时间】:2023-04-04 12:24:01
【问题描述】:

我正在尝试将我的 AngularJs 的第一个花絮配置为一些琐碎的东西,但不幸的是在相当长的一段时间后没有成功。

我的前提:
用户从下拉列表中选择一个选项,并将适当的模板加载到选择下方的 div 中。我已经设置了服务,一个自定义指令(通过@Josh David Miller 在这个post 上的 ans,以及一个适当的控制器。服务中的 ajax 调用工作正常,除了我传递给服务器的参数是硬编码的。我希望这是用户选择的下拉列表中的“键”。目前我无法将此代码传递给服务。

我的配置:

    var firstModule = angular.module('myNgApp', []);

    // service that will request a server for a template
    firstModule.factory( 'katTplLoadingService', function ($http) {

        return function() {


            $http.get("${createLink(controller:'kats', action:'loadBreedInfo')}", {params:{'b1'}}
            ).success(function(template, status, headers, config){
                return template

            })
        };
    });


    firstModule.controller('KatController', function($scope, katTplLoadingService) {
        $scope.breed = {code:''}

        // here I am unsuccessfully trying to set the user selected code to a var in service, 

        //var objService = new katTplLoadingService();
        //objService.breedCode({code: $scope.breed.code});


        $scope.loadBreedData = function(){
            $scope.template = katTplLoadingService();
        }
    });



    firstModule.directive('showBreed', function ($compile) {
        return {
            scope: true,
            link: function (scope, element, attrs) {
                var el;
                attrs.$observe( 'template', function (tpl) {

                    if (angular.isDefined(tpl)) {

                        el = $compile(tpl)(scope);
                        element.html("");
                        element.append(el);
                    }
                });
            }
        };
    })

HTML 设置是

<form ng-controller="KatController">

   <select name="catBreeds" from="${breedList}" ng-change="loadBreedData()"
         ng-model="breed.code" />

   <div>

      <div show-breed template="{{template}}"></div>

   </div>

</form>

我需要 $http ajax 调用中当前硬编码的值“b1”作为 $scope.breed.code 中的值。

【问题讨论】:

    标签: angularjs angularjs-service


    【解决方案1】:

    我认为您定义 factory 的方式不正确。试试这个:

    firstModule.factory('katTplLoadingService', ['$resource', '$q', function ($resource, $q) {
        var factory = {
            query: function (selectedSubject) {
                $http.get("${createLink(controller:'kats', action:'loadBreedInfo')}", {
                    params: {
                        'b1'
                    }
                }).success(function (template, status, headers, config) {
                    return template;
                })
            }
        }
        return factory;
    }]);
    
    
    firstModule.controller('KatController', function($scope, katTplLoadingService) {
    
        $scope.breed = {code:''}    
    
        $scope.loadBreedData = function(){
            $scope.template = katTplLoadingService.query({code: $scope.breed.code});
        }
    });
    

    【讨论】:

    • 我刚刚在@roland 的帮助下修好了。不过还是谢谢你的时间!!
    【解决方案2】:

    您的 ajax 请求是异步的,而您的控制器的行为就像请求是同步的一样。

    我假设 get 请求具有正确执行所需的一切。

    首先将回调传递给您的服务(注意 fn 的用法):

    firstModule.factory( 'katTplLoadingService', function ($http) {
        return { 
            fn: function(code, callback) { //note the callback argument
                $http.get("${createLink(controller:'kats', action:'loadBreedInfo')}",
                params:{code: code}}) //place your code argument here
                    .success(function (template, status, headers, config) {
                        callback(template); //pass the result to your callback
                    });
            };
        };
    });
    

    在你的控制器中:

    $scope.loadBreedData = function() {
        katTplLoadingService.fn($scope.breed.code, function(tmpl) { //note the tmpl argument
            $scope.template = tmpl;
        });
    }
    

    这样做,您的代码现在正在处理您的异步获取请求。

    我没有测试它,但它一定可以完成这项工作。

    【讨论】:

    • 谢谢!!解决了第一个难题...我在问题中打错了...我刚刚将其编辑为{params:{code:'b1'}}。有什么方法可以将这个硬编码值“b1”从控制器传递过来?这个值应该是$scope.breed.code中设置的值
    • 这肯定行得通!!!非常感谢您...花了很多时间来解决这个问题...显然我是菜鸟!!
    • 不知道为什么要引入 fn 函数。如果他把它称为一个承诺,他第一次写的函数就很好了。这意味着: $scope.template = katTplLoadingService();应该改成:katTplLoadingService().then(function(result){ $scope.template = result; });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多