我想使用AngularJS获取特定url参数的值。然后我想给特定的文本框分配一个特定的值。如何使用AngularJS读取网址查询参数?
一个例子URL可能看起来像:
http://example.com/?param1=value1
我见过约$位置,路由和服务的例子。我不想这样做。我只需要param1的值。任何想法如何可以做到?
这里的几个尝试相应的jsfiddle.net:http://jsfiddle.net/PT5BG/211/
我想使用AngularJS获取特定url参数的值。然后我想给特定的文本框分配一个特定的值。如何使用AngularJS读取网址查询参数?
一个例子URL可能看起来像:
http://example.com/?param1=value1
我见过约$位置,路由和服务的例子。我不想这样做。我只需要param1的值。任何想法如何可以做到?
这里的几个尝试相应的jsfiddle.net:http://jsfiddle.net/PT5BG/211/
如果使用ngRoute,寻找routeParams
如果您在使用UI的路由器,寻找stateParams
JS方式:
var key = 'param1';
var value = window.location.search.substring(window.location.search.indexOf(key)+key.length+1);
使用$ location是角度的方式
$location.search().param1;
应该给它,如果html5mode = true
否则,你必须使用纯JavaScript。检查这个。 How can I get a specific parameter from location.search?
对于html5mode,这条线在哪里? $ locationProvider.html5Mode(true).hashPrefix('!')我不太确定hashbang用于什么。我将通过问号分隔url键/值。 – 4thSpace 2014-12-13 06:47:00
尝试this.You需要路由概念角度JS
<!DOCTYPE html>
<html lang="en">
<head>
<title>AngularJS Routes example</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular-route.min.js"></script>
</head>
<body ng-app="sampleApp">
<a href="#/route1/abcd">Route 1 + param</a><br/>
<a href="#/route2/1234">Route 2 + param</a><br/>
{{param}}
<div ng-view></div>
<script>
var module = angular.module("sampleApp", ['ngRoute']);
module.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/route1/:param', {
templateUrl: 'your_url1',
controller: 'RouteController'
}).
when('/route2/:param', {
templateUrl: 'your_url2',
controller: 'RouteController'
}).
otherwise({
redirectTo: '/'
});
}]);
module.controller("RouteController", function($scope, $routeParams) {
$scope.param = $routeParams.param;
alert($scope.param);
})
</script>
</body>
</html>
$ routeParams让你DTO参数
由于首次使用了wordpress进程,这不起作用。它需要是查询参数。 – 4thSpace 2014-12-13 15:56:35
作为OP提到没有路由的值。 – 4thSpace 2014-12-13 06:47:15