If I have a typical route like this:
angular.module('app', ['ngResource']).
config(function($routeProvider, $locationProvider) {
.when('/item/:itemId', {
template: '<div ng-bind-html-unsafe="html"></div>',
controller: blobs.controller.RouteController,
})
});
How can I achieve requesting a 'dynamic view' from the server? ie. the server's route should lead to a dynamic php page, fill in and preserve some data, and return whatever html it wants, based on the given itemId in the route. I tried setting up some complicated route monitoring and ajax requests for a template container, and then injecting the response html, on load of the RouteController, ie:
Service.request('item/'+$routeParams.itemId, {}, function(r) {
$scope.html = r;
$compile($scope.html)($scope); //generates infinite loop for some reason?
$scope.$apply();
});
But this isn't quite working. The PHP's response html get's loaded into the container template just fine, but the data binding doesn't seem to work (ie. it contains {{*}} everywhere).
Is there a better way to do this? I'm really just trying to turn a route like this:
/item/My-Item into a "templateUrl" that is the same, ie: /item/My-Item or /views/item/My-Item ..etc
Thank you for any help!
Ryan
It's not a direct answer to your question. But I think it's an important concept, that you should consider.
In order to use AngularJS to it's fullest potential you have to completely understand it's paradigm.
Angular is all about thin server and a fat client. Server is mostly a RESTful service that supplies your front-end application with data. That data is then rendered to the view by means of bindings. Templates are processed client-side, not server-side. This approach allows you to better split responsibilities between server and client.
So, considering your use-case I would recommend to move "dynamic" logic of your templates from server-side to client-side and try not to mix them. Ideally, your templates should be a static files, that can be served by some CDN. All data that your server-side logic depends on to render the dynamic template can be fetched from the server using ngResource
module and RESTfull interface. You can achieve almost any rendering logic by using appropriate bindings and directives client-side. You can even create your own directives to isolate repeatable parts of your code and DRY things up.
I hope it helps.