How to get dynamic value from url
I am learning AngularJS from there. I want to download content from MySQL with a link to a url value.
The RoutingProvider method we used used in AngularJS:
$routeProvider.when('/page/pages=:pages', {
templateUrl: 'page.html',
reloadOnSearch: false
});
My dynamic url:
"<a class='list-group-item' href='#/page/pages=" + slug + "'>" +
title + " <i class='fa fa-chevron-right pull-right'></i>" +
"</a>"
After that I tried to alert the location of the url in PhoneGap (screenshot attached)
Now I want to pass this page value to AJAX, so I am getting a query for results from 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
}
});
});
I am not getting any output.
source to share
To get request parameters in your url use AngularJS $ location provider . If your url looks like example.com/#/page?pages=5, you can get the value 5 by writing
var urlPageValue = $location.search()["pages"];
Make sure to include the $ location and $ http services in your controller by writing
yourModuleName.controller("AwesomeController", ["$location", "$http", function($location, $http){
// Page initialization code (like your ajax call) here
}])
Then, as Szanto suggested, use the $ http service to make your ajax call by writing
$http({
url:"/yourApiEndpoint",
method: "POST",
data: { pages: urlPageValue }
}).success(function(response){
// Handle returned data here
}).error(function(){
// Handle errors here
})
Finally, in the original question, when you returned data from your SQL query, you were manually adding spacing to the DOM. If you are going to use AngularJS you should assign the response to an array variable and use the ngRepeat directive in your HTML to instantiate some kind of template for each element in your array.
source to share