[英]How to get dynamic value from URL
我正在研究AngularJS,我希望从URL中加载MySQL中的内容。
我使用的AngularJS RouteProvider是这样的:
$routeProvider.when('/page/pages=:pages', {
templateUrl: 'page.html',
reloadOnSearch: false
});
我的动态网址是:
"<a class='list-group-item' href='#/page/pages=" + slug + "'>" +
title + " <i class='fa fa-chevron-right pull-right'></i>" +
"</a>"
之后,我尝试提醒PhoneGap上的URL位置(附上截图)
现在,我想将此页面值传递给AJAX,以便从MySQL获得结果查询。
$(function ()
{
//-----------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-----------------------------------------------------------------------
jQuery.ajax({
url: 'http://keralapsctuts.com/app/index.php', //the script to call to get data
data: "pages="+pages, //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
type: "POST",
dataType: 'json', //data format
success:function(data) {
var result = "";
var title = "";
alert(pages);
for(var i=0; i < data.length; i++) {
var title = data[i]["pagetitle"]; //get name
var content = data[i]["pagecontent"]; //get slug
result += +content;
title += "<span>"+title+"</span>";
}
$('#pcontent').html(content); //Set output element html
$('#ptitle').html(title); //Set output element html
}
});
});
我没有得到任何输出。
要获取URL中的查询参数,请使用AngularJS $位置提供程序。 如果您的网址看起来像example.com/#/page?pages=5,则可以通过编写获取值5
var urlPageValue = $location.search()["pages"];
请务必通过编写在控制器中包含$ location和$ http服务
yourModuleName.controller("AwesomeController", ["$location", "$http", function($location, $http){
// Page initialization code (like your ajax call) here
}])
然后,正如Szanto建议的那样,使用$ http服务通过写入来执行ajax调用
$http({
url:"/yourApiEndpoint",
method: "POST",
data: { pages: urlPageValue }
}).success(function(response){
// Handle returned data here
}).error(function(){
// Handle errors here
})
最后,在您的原始问题中,当您从SQL调用中获取数据时,您手动将跨度添加到DOM。 如果要使用AngularJS,则应将响应分配给数组变量,并使用HTML中的ngRepeat指令为数组中的每个项实例化一些模板。
你正在使用Angular和jQuery? 这是一个坏主意,使用Angular的内置http 方法来替换jQuery ajax请求。 其次,您可以将参数传递给请求配置(即使在jQuery中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.