最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to inject $httpParamSerializer for use in templateUrl - Stack Overflow

programmeradmin1浏览0评论

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.

Example code:

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 ) {

Also in the moduler, like this:

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

Any insight why this isn't working would help.

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.

Example code:

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 ) {

Also in the moduler, like this:

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

Any insight why this isn't working would help.

Share Improve this question edited Mar 2, 2016 at 19:50 Pankaj Parkar 136k23 gold badges240 silver badges303 bronze badges asked Mar 2, 2016 at 19:41 DimitryDimitry 4,5936 gold badges28 silver badges40 bronze badges 1
  • 1 I created JS Fiddle to play around with it: jsfiddle/kh9oeq1y/15 – Dimitry Commented Mar 2, 2016 at 19:55
Add a ment  | 

2 Answers 2

Reset to default 4

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
      }
  });
});

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

This forum suggests it's a problem with your angular version. Check that you're using at least 1.4.0.

发布评论

评论列表(0)

  1. 暂无评论