M-S M-S - 3 months ago 8
Javascript Question

How to display the span once the value is entered in input field? Angular JS

I have a scope variable defined in my controller, initially the value of my scope variable is empty.

I binded the ng-model to the input field and added ng-show to the span which needs to be display, once the value is entered in the input field.

When I entered a value in the input field, the span is not getting displayed.

Code:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-ng-form-production</title>


<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script>


<script type="text/javascript">
angular.element(document.getElementsByTagName('head')).append(angular.element('<base href="' + window.location.pathname + '" />'));
</script>
</head>
<body ng-app="formExample">
<script>
angular.module('formExample', [])
.controller('FormController', ['$scope', function($scope) {
$scope.userType = '';
}]);
</script>
<form name="myForm" ng-controller="FormController" class="my-form">
userType: <input name="input" ng-model="userType">
<span ng-show="myForm.input.length>0">show me!</span><br>
</form>
</body>
</html>


Here is the demo Link:

Demo Plunker

Answer

You need to check the scope variable userType's length, It should be,

<span ng-show="userType.length>0">show me!</span><br>

DEMO APP