0

I am developing bridge like interface for jquery grid control. The grid is rendered with the below syntax and works as expected.

<t-grid>
  <t-column>...</t-column>
  <t-column>...</t-column>
</t-grid>

While providing support to render template inside the t-column tag, I am able to get the data and jquery element.

jQuery element

<div class="angulartmplbda8aedb-6b16-456d-8c17-3240a674b0c7 angular-template">
   <div _ngcontent-ila-1="">
     <input _ngcontent-ila-1="" type="button" value="Template"></div>
 </div>

Now, the button is displayed with template text. How to dynamically change the input element value which is from gridData.?

 export class TemplateElement {
private context: any;
__parent: tComponents<any, any>; 
constructor(protected el: ElementRef) {
}

ngOnInit() {
    template.render = (self, selector, data, index, prop) => {
        let templateObject = self.angularTemplate;
        if (!templateObject || !templateObject[selector]) {
            templateObject = templateObject || {};
            templateObject[selector] = { key: t.getGuid('angulartmpl'), itemData: [], views: [] };
            self.angularTemplate = templateObject;
        }
        let scope = templateObject[selector];
        if (!t.isNullOrUndefined(index)) {
            if (!scope.itemData)
                scope.itemData = [];
            scope.itemData[index] = data;
        } else {
            scope.itemData = [data];
        }
        let actElement = $(this.el.nativeElement).html();
        let tempElement = "<div class='" + templateObject[selector].key + " t-angular-template'>" + actElement + '</div>';
        return tempElement;
    }
}
ngAfterViewInit() {
    this.compileTempalte();
}

compileTempalte() {
    let element = this.__parent.widget.element;
    let templates = $(element).find('.t-angular-template');
    let templateObject = this.__parent.widget.angularTemplate;
    for (let template in templateObject) {
        let tmplElement = templates.filter('.' + templateObject[template].key);
        if (tmplElement.length) {
            for (let i = 0; i < tmplElement.length; i++) {
               //modified code 
               childView = this.viewContainerRef.createEmbeddedView(this.templateRef, { '$implicit': templateObject[template].itemData[i] });                    
                this.childViews[i] = childView;
                $(tmplElement[i]).append(childView.rootNodes);
            }
        } else {
            delete templateObject[template];
        }
    }
}

clearTempalte() {
    let templateObject = this.__parent.widget.angularTemplate;
    if (templateObject && Object.keys(templateObject).length) {
        for (let tmpl in templateObject) {
            delete templateObject[tmpl];
        }
    }
}
ngOnDestroy(){
    this.clearTempalte()
}

}

7
  • 1
    Could you please add a working plunker? Commented Oct 17, 2016 at 12:27
  • Here is the plunker for the requirement. plnkr.co/edit/vJHUCnsJB7cwNJr2cCwp?p=preview Commented Oct 18, 2016 at 5:36
  • @yurzui is there any possibilities to compile the element with data.? Commented Oct 18, 2016 at 11:41
  • 1
    See also stackoverflow.com/questions/38607075/… Commented Oct 19, 2016 at 5:54
  • Thanks a lot @yurzui I sorted out cause. Commented Oct 19, 2016 at 5:58

2 Answers 2

1

As discussed, you need to use transclusion:

And you need to add let-item="$implicit" on this part:

<template t-template let-item="$implicit">
    <input t-button [value]="item.CustomerID" />
</template>
Sign up to request clarification or add additional context in comments.

5 Comments

I am looking for dynamically compile the innerHTML from <t-template>....</t-template> tag. I am able to get the innerHTML and gridData in ngAfterViewInit method.i.e. <t-template><div>{{data.value}}</div></t-template>, in this template how can I compile the expression.?
What you're trying to do is called transclusion: toddmotto.com/transclusion-in-angular-2-with-ng-content
I did some analysis and I can able to process the innerHTML and render the same in grid cell using EmbeddedView which I have updated in the query. childView = this.viewContainerRef.createEmbeddedView(this.templateRef, { '$implicit': templateObject[template].itemData[i] }); this.childViews[i] = childView; $(tmplElement[i]).append(childView.rootNodes); But the dynamic data compilation is not working. Am I doing anything wrong.?
OK... It is difficult to understand your English. However, I'm just not sure what you mean by "the dynamic data compilation is not working." What dynamic data compilation? Unfortunately, I have no idea what you mean.
Thanks for your comments. I resolved the issue using ViewContainerRef and TemplateRef
1

If you want to assign a variable to a property you need to use the bracket syntax:

     <input _ngcontent-ila-1="" type="button" [value]="Template"></div>

The [] means, that the variable Template will be assigned to the value attribute of the input element.

As explained here: https://angular.io/docs/ts/latest/guide/template-syntax.html#!#binding-syntax

3 Comments

I am not looking for the syntax. I am having data and jquery element in the directive <t-template>. I am looking for how to dynamically compile the both element and data in ngAfterViewInit life-cycle hook.
I don't understand what you are trying to do, please provide more code and info.
update the query with TemplateElement class. In which how to compile the template with data in compileTemplate() method. The template might be anything (Checkbox, Dropdown, etc.) which is going to be placed in gridcell.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.