在 $location.path 中以角度传递参数

我只是尝试在我的控制器中使用 $location.path(),但也传递一个自定义变量作为参数。我猜它看起来是这样的:

$scope.parameter = 'Foo';


$location.path('/myURL/' + $scope.parameter);

但这不管用,有人知道角度是怎么做到的吗?

91456 次浏览

to add Parameters you should use $location.search() method so:

$location.path('/myURL/').search({param: 'value'});

$location methods are chainable.

this produce :

/myURL/?param=value

The another way to add parameter to url is:

 $location.path('/myURL/'+ param1);

and you can define route to myPage.html:

config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/myURL/:param1', {
templateUrl: 'path/myPage.html',
controller: newController
});
}]);

The parameter can then be accessed in newController as follows:

var param1= $routeParams.param1;

For example if you need to put in your URL one or more parameters:

$location.path('/path').search({foo: 'valueFoo', baz:'valueBaz'})

in your url will represent

/path?foo=valueFoo&baz=valueBaz

To get params in an other controller:

var urlParams = $location.search();




urlParams.foo will return valueFoo


urlParams.baz will return valueBaz
  function pathToSomewhere() {
$stateParams.name= vm.name; //john
$stateParams.phone= vm.phone; //1234
$stateParams.dateOfBirth= getDoB(); //10-10-1990


$location.path("/somewhere/").search($stateParams);


};

This results in the url

http://middle-of-nowhere.com/#/somewhere/?name=john&phone=1234&dateOfBirth=10-10-1990

This way you don't have to manually type out the parameters inside brackets