Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have AngularJS controller:

$scope.name = "Ann";
$scope.test = "Hello, {{name}}!";

var element = document.createElement("input");
element.setAttribute('ng-model', "name");
document.getElementById("preview").appendChild(element);

And HTML-code:

<div id="preview">
    <p ng-bind-html="test"></p>
</div>
{{name}}

But at page I see Hello, {{name}}!, empty input and Ann name. How to setting input and div for show name? That I change name in input and name changed in div?

share|improve this question
up vote 1 down vote accepted

Checkout this plnkr

 $scope.name = "Ann";
$scope.test = "Hello, {{name}}!";

var element = document.createElement("input");
element.setAttribute('ng-model', "name");
document.getElementById("preview").appendChild(element);

$compile(document.getElementById("preview"))($scope);
share|improve this answer
    
Ok! So how to edit name in "Hello, {{name}}! ? – TestUser Jan 12 at 10:50
    
updated my plnkr.. Check it.. – dhavalcengg Jan 12 at 10:52
    
It isn't a true solution for me, because the $scope.test is dynamic string. And I don't know, how many {{name}} I have – TestUser Jan 12 at 10:58
    
It will be updated when your $scope.name will update. Your Hello 'name' is sync to your input box. So whatever there in $scope.name will be shown. :) – dhavalcengg Jan 12 at 11:01

ng-html compile does not compile bindings but only parses the static html. If you want to also have your syntax compiled in strings you need to implement custom directive for that:

app.directive('ngHtmlCompile', ["$compile", function ($compile) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            scope.$watch(attrs.ngHtmlCompile, function (newValue, oldValue) {
                element.html(newValue);
                $compile(element.contents())(scope);
            });
        }
    }
}]);

and then you can compile:

<div ng-html-compile="Hello, {{name}}!"></div>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.