如何注入$ httpParamSerializer以在templateUrl中使用 [英] How to inject $httpParamSerializer for use in 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){
同样在模块中,如下所示:
Also in the moduler, like this:
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中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!