HTML:
<body ng-controller="TestController as _tc">
<h4>Controllers value:</h4>
<p>{{_tc.value}}</p>
<button ng-click="_tc.addInput()">Add Directive Value</button>
<test-directive></test-directive>
</body>
JS:
var app = angular.module('plunker', []);
var testController = function( testService ) {
this.value = 'Test Value!';
this.content = '';
this.addInput = function() {
testService.add('<input placeholder="Enter value" ng-model="_tc.value" />');
}
};
var testService = function( $sce ) {
this.content = '';
this.add = function( content ) {
this.content = $sce.trustAsHtml( content );
}
};
var testDirective = function( testService ) {
return {
template:'<h4 ng-if="data.content !== \'\'">If you enter value below it should be bound to the controllers value above because scope is shared but value is not binding</h4><div ng-bind-html="data.content"></div>',
link: function( $scope ) {
$scope.data = testService;
}
}
};
testService.$inject = ['$sce'];
app.service( 'testService', testService );
testController.$inject = ['testService'];
app.controller('TestController', testController );
testDirective.$inject = ['testService'];
app.directive( 'testDirective', testDirective );
Plunkr: http://plnkr.co/edit/rqnFjtYZ0s4wdOlBpEV1?p=preview
The problem: When the directives content is added the data binding is not working in the directive.
I understand that the dynamically added content is only text and can not bind any values but i don't know how to compile it proper way for it to work as supposed to.
Any help will be really appreciated