techPackets
techPackets

Reputation: 4506

Angular Js binding text input with model

I am using a very simple code to bind the user text input to the model. The text box prompts the user to enter the name & says hello "username" on enter. But I also want Hello to appear only when the user enters the name. Can anyone help me with that. Currently Hello is present by default.

ui page

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script type="text/javascript" src="../js/prac_controller.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>prac_one</title>
</head>
<body ng-app>

<div ng-controller="pracOne">
Enter the name<br>
<input type="text" ng-model="message.text"/>
<p>Hello, {{message.text}}</p>
</div>
</body>
</html>

controller

function pracOne($scope)
{
$scope.message={text:""};
}

Upvotes: 0

Views: 417

Answers (1)

AlwaysALearner
AlwaysALearner

Reputation: 43947

<p>{{message.text ? 'Hello, ' + message.text : message.text}}</p>   

Or

<p>{{message.text && 'Hello, '+message.text || message.text}}</p>

Update: You can use ngShow if you want to conditionally display an element:

<p><span ng-show="message.text != ''">Hello, </span>{{message.text}}</p>

Upvotes: 1

Related Questions