I'm pulling JSON data from a server. Inside the JSON object I'm returning, I have strings that I want to be able to use placeholders with to dynamically insert values entered by the user in the UI.
My understanding is that this is what $compile is for, but I can't seem to get that to work. I'm not sure if this is possible or if I'm just approaching it the wrong way.
Edit: Not sure if I'm explaining well. Got a little further and I updated the Plunk and the code below
A simplified example (view Plunk):
View:
<body ng-controller="MainCtrl">
<input ng-model="name" type="text" />
<p ng-bind-html="myval">{{myval}}</p>
<p>{{name}}</p>
</body>
Angular App:
var app = angular.module('plunker', ['ngSanitize']);
app.controller('MainCtrl', function($scope, $compile, dataSvc) {
init();
function init() {
$scope.data = dataSvc.getData();
$scope.name = '';
}
$scope.$watch('name', function(newVal, oldVal) {
var c = $compile('<span>' + $scope.data.vals[0].text + '</span>')($scope);
console.log(c);
console.log(c[0]);
$scope.myval = c[0];
});
});
app.service('dataSvc', function () {
this.getData = function () {
return {
vals: [
{
text: "Hello {{name}}"
}
]
}
};
});
This almost works with $compile and the console logs the changes the way I want them to happen, I just can't get it to output on the display.
$digest
cycle is already complete. you could use a directive and inject the element by hand, but really, there are few reasons that you would store client side expressions in their raw state in the database in the first place.