Set TemplateRef value from component

Shohel

I am using a third party plugin "angular-mention", Which is taken a template the following ways.

<ng-template #mentionListTemplate let-item="item">
  <div class="media">
    <img class="mr-3" src="{{item.avatar}}" alt="" width="30" height="30">
    <div class="media-body text-right">
      <h6 class="mt-0 mb-0">{{item.name}}</h6>
      <small>@{{item.username}}</small>
    </div>
  </div>
</ng-template>

The template is bind this way.

<textarea [mentionListTemplate]="mentionListTemplate"</textarea>

My problem is, I have lots of angular mention setting with textarea, every place needs mentionListTemplate so it is very much difficult to manage template setting for all places. Now I want to provide this mentionListTemplate from one place by using service or directive.

How could I pass mentionListTemplate from the component ? Thanks.

shohrukh

You can create a reusable component with input parameter for item:

@Component({
  selector: 'app-textarea',
  template: `
    <textarea [mentionListTemplate]="mentionListTemplate"></textarea>
    <ng-template #mentionListTemplate let-item="item">
      <div class="media">
        ...
      </div>
    </ng-template>
  `
})
export class TextareaComponent  {
  @Input() item: IItem = { /* some default value */ };
}

So you can easily use it in other components: <app-textarea [item]="someItem"></app-textarea>

p.s. also you can extend this component and create custom form control by implementing ControlValueAccessor. In this case your textarea will behave like a usual form control element. For more details, checkout this article.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use `templateref` from other Angular component?

rxjs behavior subject set value from component

Getting templateRef of host component in directive

How to access TemplateRef on the component of Angular?

ZK Component Set Component Not Value

Angular ng-bootstrap Modal Open Doesn't Support TemplateRef as Custom Component Passed from Template

How to pass value to props from component and set state

Child component does not set the initial value passed from the parent: ReactJS

Java: How to set a value from javascript to a wicket component textfield

How to set dynamic value in a useState from parent component

How to set a default value for a Select component from react with MenuItem as options

How to Set Value in Formik from custom component in react native

Angular 2: How to set default value of field from parent component back to child component?

How to get TemplateRef of a component in angular2?

How to set the value of a component to a number

Set React component state from outside of component

Set state in component from input

Passing state value from component to component?

Passing value from callback in component to another component

how to set background image of a div whose value is coming from a dialog(image url) in sightly component AEM?

How can I set the value of a component's variable from the router, in React 18?

How to set defaultChecked getting value from state React Functional Component Checkbox (React - Hooks)

Creating/Fetching a TemplateRef from within a directive

Passing value from component to JS

Return value from react component

Retrun a value from another component?

PrimeNG - set value in time-picker component

Set default value for the select component in pentaho CDE

Set Antd TextArea value to Tag component