I have a simple javascript code that dynamically adds HTML content to a div tag.
function addData(id,title,title_data){
var htmlcode='';
htmlcode+=`<h1> Title: `+title+` </h1> <div ng-app="">
<input ng-model="model`+title+id`" ng-init="model`+title+id+'='+\'`+title_data+`\'`" >`+
`<p ng-bind="model`+title+id`"> </p>`;
return htmlcode;
}
Then i simply use it like this:
$('#someDiv').html(addData(1,'Some Title','Some title data'));
The html works well and displays the desired dynamic content. But the angular bindings didn't work. The copy pasted version of the rendered html from web browser works well including the bindings but the dynamic one does not. What seems to be the problem?
$('#someDiv')is jQuery code, not angular; trying to use angular in this way is highly irregular, and not recommended. This seems to indicate that perhaps your overall app design isn't fully embracing angular. If you are able to re-factor your app to remove jQuery and still find that you need HTML strings like this (rather than angular templates or simillar), then, and only then, should you look at how to compile dynamic expressions.ng-app==="".ng-appto the DOM after the page is finished loading doesn't tell angular that it should go looking for that element. if you are insistent that this is the only way you can develop this app, you will be responsible for manually bootstrapping the app (docs.angularjs.org/guide/bootstrap), but this generally means you need to know a bit more about how the framework works, and after you spend time learning about it, you'll realize how much of a terrible idea this really is.