Search code examples
javascriptangularjsionic-frameworkngcordovaionic-view

How to bind a string to scope var within the view?


I want to bind a string to a scope var which will get its value by user input in an input field inside a view. So that the string concatenated to the scope var value should display in another div in the view.

It should be something like that:

<body ng-controller="MainCtrl">
Name: <input ng-model="userInput" placeholder="Enter your input..."/><br>
<div>{{ "Hello" + userInput}}</div>

But the problem here is that the word "Hello" displays already before the user input is made. I want to display it together with the value of the scope var when the user input it.


Solution

  • Can use numerous different approaches ... ng-if or use a ternary in expression are two of them

    <div ng-if="userInput">{{ "Hello" + userInput}}</div>
    

    Or

    <div>{{userInput ?  "Hello" + userInput :''}}</div>