Is it a good practice to use Function calls inside {{}} (Curly Brackets)?
Is there any way to do this? within the component, for example (maybe using ngOnChanges
or anything like that...)
Template
<div class="container">
{{ bootstrap() }}
{{ validate() }}
<textarea class="form-control">{{ fullHtml }}</textarea>
<button (click)="copy()" class="btn btn-primary">Click to Copy HTML</button>
<textarea class="form-control">{{ validator }}</textarea>
<button (click)="copy()" class="btn btn-warning">Click to Copy Validate</button>
<button class="btn btn-danger" (click)="add()">Add page and input</button>
</div>
Component
class HomeComponent {
fullHtml = "";
validator = "";
pages = [{
"name": "Page 1"
},{
"name": "Page 2"
}];
inputs = [{
"name": "input_1",
"required": true
},{
"name": "input_2",
"required": false
}];
public bootstrap() {
this.fullHtml = this.pages.map((page, pageNumber) => {
return '<div class="row">' +
page.name +
'</div>'
}).join('')
}
public validate(){
this.validator = this.inputs.map((input, i) => {
return '"' + input.name + '" => [' + (input.required? 'required': 'nullable') + '],\n';
}).join('')
}
public copy(){
alert("under construction");
}
public add(){
this.pages.push({
name: "page 3"
});
this.inputs.push({
"name": "input_3",
"required": true
});
}
}
https://jsfiddle.net/1hk7knwq/10283/
ps. I need to display HTML content in a textarea, that's why I'm doing the html inside the component.
Braces or curly brackets { } are used when the domain or range consists of discrete numbers and not an interval of values. If the domain or range of a function is all numbers, the notation includes negative and positive infinity .
Curly brackets are rarely used in prose and have no widely accepted use in formal writing, but may be used to mark words or sentences that should be taken as a group, to avoid confusion when other types of brackets are already in use, or for a special purpose specific to the publication (such as in a dictionary).
In languages like C curly braces ( {} ) are used to create program blocks used in flow control. In Python, curly braces are used to define a data structure called a dictionary (a key/value mapping), while white space indentation is used to define program blocks. Save this answer.
Without curly brackets, you could accidentally write a semicolon after the IF-statements. The semicolon is a valid, empty statement, which will be "execute" instead of the actual (intended) one.
The answer for your question is: it depends.
If the execution time of the function is short, that's fine. In case the function includes many complex calculation which take a quite long time to finish, then it might cause a serious problem with the user experience.
This is what the Angular team said in their official document:
Quick execution
Angular executes template expressions after every change detection cycle. Change detection cycles are triggered by many asynchronous activities such as promise resolutions, http results, timer events, keypresses and mouse moves.
Expressions should finish quickly or the user experience may drag, especially on slower devices. Consider caching values when their computation is expensive.
For reference: https://angular.io/guide/template-syntax#quick-execution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With