2

I just a problem about how to set space to my value in html. I have code :

    setValuetoComboBox(test:string, lvl:number){
    let lastTest:string;
    let spaceString:string;
    // 
    spaceString='&#160';
    for(var i = 1; i <= lvl; i++){
        spaceString=spaceString + '&#160';
    }
    lastTest = spaceString + test;
    return lastTest;
}

In this code, i have &#160 which to make space in html. and i have code in html:

                                    <select [formControl]="parent" class="form-control">
                                    <option value="-1">Root</option>
                                    <option value="{{test.id}}" *ngFor="let test of parentValue">
                                        {{setValuetoComboBox(test.name, test.lvl)}}
                                    </option>
                                </select>

and the result is :

The Result

this the wrong result, i want space not code &#160. Can anyone find the answer about my problem ?

1
  • Use ' ' instead of the code? Commented Dec 21, 2016 at 6:54

1 Answer 1

2

I guess what you actually want is

<option value="{{test.id}}" *ngFor="let test of parentValue"
    [innerHTML]="setValuetoComboBox(test.name, test.lvl)">
</option>

With {{ }} the literal string will be inserted.

You might need to mark it as secure using the sanitizer for example like shown in How to bind raw html in Angular2

You should be aware that setValuetoComboBox(...) might be called very often (every time Angular runs change detection). It's usually a good idea to assign the results of the call in a property and bind to this property instead of binding to a function.

Binding to functions in the view is generally discouraged because of this. It can cause serious performance issues.

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

Comments

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.