In part of my application, I have a user editing a single page of information (via an HTML editor: Content Tools). I have started to modify that functional code so that the page they are editing is structured with a dynamic template, rather than hard coded.
Currently I am storing the edited/editable data in a database table. I also am storing the template definition in a database table. The template I am using is simply the HTML code that was previously hard coded, so I know the template is valid.
At this point I am correctly receiving back all the data in a JSON wrapper from the server just fine. What is not working is rendering the content--the page remains blank. I'm sure the issue is my lack of understanding for AngularJS and how it executes.
[UPDATED: Code and following execution description has been updated after reworking with input from @Wes H and @31PIY. No longer a $templateCache issue, apparently.] Specifically, my HTML is:
<body ng-app>
<div ng-controller="PageEditController">
<div ng-bind-html="renderHtml(htmlTemplate)"></div>
</div>
</body>
[NOTE: renderHtml is my wrapper for $sce.TrustAsHtml() to make sure the HTML is not stripped by Angular]
And the relevant part of my javascript is:
$http({
method: 'POST',
url: 'https://my-api-access-point',
data: parms
}).then(function successCallback(response) {
var resp = response.data['Items'];
var data = {};
if (resp)
data = resp[0];
console.log("jsLoadScreen Data: ", data);
$scope.models.fields = data;
console.log('replacing template: ', $scope.models.fields['template']);
$scope.htmlTemplate = $scope.models.fields['template'];
And the console output is:
jsLoadScreen Data: {UID:"1"
left:"<h1>↵ One↵</h1>"
right:"<h2>↵ Two↵</h2>"
screenId:"1"
template:" <div class="row">↵ <div class="column">↵ <div data-editable data-name="left" ng-bind-html="renderHtml(models.fields.left)">↵ </div>↵ </div>↵ <div class="vl"></div>↵ <div class="column">↵ <div data-editable data-name="right" ng-bind-html="renderHtml(models.fields.right)">↵ </div>↵ </div>↵ </div>"
replacing template: <div class="row">
<div class="column">
<div data-editable data-name="left" ng-bind-html="renderHtml(models.fields.left)">
</div>
</div>
<div class="vl"></div>
<div class="column">
<div data-editable data-name="right" ng-bind-html="renderHtml(models.fields.right)">
</div>
</div>
</div>
I have verified that the template code IS loading on the page (via "view source"). However, no javascript or AngularJS tag in that template code is executed once it loads.
I have tried several steps to make this code execute, including adding another .then()
to my promise to touch some variables that are in the template to force a refresh, as well as various ill fated attempts to cause delays in the process in case there was a race condition. No luck.
Am I in such strange waters for AngularJS that I should just give up and combine the template and data on the server side and make this a single AJAX operation and not try to do the two step: (1) AJAX insert new HTML template, once inserted then (2) AJAX insert new data into the new HTML template? Or is there something I should do to the newly inserted template (or while inserting the template) to register it as something for javascript to execute?
The closest descriptions I have seen on SO are: How $templateCache works? and Load HTML template from file into a variable in AngularJs but I can't quite wrap my mind around what they say and what I am unsuccessfully trying to do.
UPDATE: After working through the previous responses below, I am closeER. With the info from @Wes H and @31piy I now have verified the HTML is loaded on the page correctly (as seen by "view source" in the developer tools on Chrome). HOWEVER, the various Angular commands (ng-bind-html(renderHtml...)) and 3rd party tool triggers (data-editable) don't appear to be interpreted by Angular, so I have the HTML but no javascript activity related to it. My guess is the inserted HTML template somehow is put in the DOM but not actually recognized and executed by Angular (or javascript). Thoughts?