1

I want to bind content from an template to data in an Angular application. The template is stored in an html file and contains binding expressions:

<div>
    <div class="case-header">
        <h3>Case Details</h3>
    </div>
    <div class="case-body">
        <div>
            <label>Case Number:</label><div>{{item.CaseNumber}}</div>
        </div>
        <div>
            <label>Name:</label><div>{{item.CandidateName}}</div>
        </div>
        ...
    </div>
</div>

The element providing the context and data for the template is a row in an ng-grid. In the function below, I create a new scope to contain the data for the current row. The contents of a template is retrieved from an html file and cached to a property when the controller is constructed. However, the $compile call does not bind the data as I want. The function returns the template markup with the binding expressions still in it. I have tried both styles of binding expression.

    $scope.getTooltip = function (row) {
        var scope = $scope.$new();
        scope.item = row.entity;
        var elem = angular.element($scope.template);
        var result = $compile(elem)(scope);
        return result.html();
    };

The call to $compile appears to be correct vis the documentation, but I guess I must have misunderstood something.

How do I get the data values injected into the template?

Plunker

Here's a Plunker to play with.

2
  • could you share you complete code in plunker or fiddle Commented Jul 3, 2013 at 16:58
  • Added and linked above in main article. Commented Jul 3, 2013 at 17:36

1 Answer 1

1

The $compile(scope) returns a reference to original dom element or clone of it so you should append it to some element . I have updated your plunker http://plnkr.co/edit/qatH4XNKPxjPyicur67T?p=preview so see it working.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @Ajay, that works in the simplified Plunker I put up to illustrate the problem and is a good start. However I want the getTooltip function to return the bound html template so that I can inject that content by binding to the function name itself. I have updated my original Plunker to show what I mean. Is there any way of forcing Angular to bind the data without appending the compilation results to a DOM object?
no you have to either make a directive or manually append dom to element to make interpolation work
Bizarre. Why would they have limited it that way?
may be you should put this in angular issues on git hub site and they will better explain it
Ajay, there is a problem with your plunker example. The binding doesn't work if you click the "Bind Template" button twice. What could be the reason?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.