Is there a way, in an AngularJS controller, to take a URL and redirect that request to the best place for fetching JSON data? The VideoSearchCtrl is connected to the search form. Everything seems fine with the generated URL for the template, so I aim to use the controller to direct it to the appropriate location for fetching the JSON data.
GuideControllers.controller('VideoSearchCtrl', ['$scope', 'VideoSearch',
function($scope, VideoSearch) {
var pattern = new RegExp(".*/search\\?=(.*)");
var params = pattern.exec( document.URL )[1];//redirect to videos to execute the search for the data
$scope.videos = VideoSearch.query({ resource: "videos", action: "search", q: params });
}
]);
This sends /videos/search?q=xyz into the query. The factory creates the resource:
var VideoSearchServices = angular.module('VideoSearchServices', ['ngResource']);
VideoSearchServices.factory('VideoSearch', ['$resource',
function($resource){
return $resource("/:resource/:action:params", {resource: "@resource", action: "@action", params: "@params"}, {
query: {
isArray: true,
method: "GET",
headers: {
"Accept": "application/json",
"X-Requested-With": "XMLHttpRequest"
}
}
});
}
]);
However, the server receives the URL as /videos/search%fq=xyz instead of /videos/search?q=xyz, resulting in the "show" method being called instead of a custom "search" action. It seems like there might be some issue with escaping characters or perhaps the "?" symbol is causing confusion in the resource factory. This could be obvious to someone experienced with AngularJS or JavaScript in general.
I already have a template for search and can successfully retrieve JSON from a different location. Both functionalities work independently, but I struggle to fetch the JSON using the provided code.