本文介绍了如何注入 $httpParamSerializer 以在 templateUrl 中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试注入 $httpParamSerializer 以在 templateUrl 中使用,但我遇到了一些问题.没有任何关于如何使用 $httpParamSerializer 的好的文档和示例.

I am trying to inject $httpParamSerializer for use in templateUrl but I am having some issues. There isn't any good documentation and examples of how to use $httpParamSerializer.

示例代码:

angular.module('myApp', ['ngRoute']).config(
  function($routeProvider, $locationProvider, $httpProvider) {
    $routeProvider
        .when('/data/dashboard', {
            templateUrl: function(params) {
             //NEED TO USE IT HERE
            }
        })
})

没用的东西,这样放置:

Things that didn't work, placing it like this:

function($routeProvider, $locationProvider, $httpProvider,$httpParamSerializer ) {

也在模块中,像这样:

angular.module('myApp', ['ngRoute', '$httpParamSerializer']).config(

了解为什么这不起作用的任何见解都会有所帮助.

Any insight why this isn't working would help.

推荐答案

由于您在配置中,您需要从提供程序本身获取它.即注入提供者并调用 $get

Since you're in the config you need to get it from the provider itself. That is, inject the provider and call $get

angular.module('myApp', ['ngRoute']).config(
  function($routeProvider, $locationProvider, $httpProvider, $httpParamSerializerProvider) {

    // get the serializer from the provider
    var paramSerializer = $httpParamSerializerProvider.$get();
    console.log(paramSerializer({a:1})); // test it

    $routeProvider.when('/', {
      templateUrl: function(params) {
        // you can use paramSerializer(here
      }
  });
});

jsfiddle 的工作版本:http://jsfiddle.net/kh9oeq1y/16/

Working version of your jsfiddle: http://jsfiddle.net/kh9oeq1y/16/

这篇关于如何注入 $httpParamSerializer 以在 templateUrl 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 03:09