I just started learning AngularJS. In a testing program I wrote, I need to check whether my input field is empty or not inside the controller(script).
Following is the code I wrote
<div ng-controller="ExampleController">
<input type="text" ng-model="userFirstName" required/><br>
<p ng-bind="msg"></p>
</div>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('ExampleController', function ($scope) {
if($scope.userFirstName.length === 0){
$scope.msg = "pls enter something";
}else{
$scope.msg = "Something Entered";
}
});
</script>
Here, as the output, I expected to see pls enter something. But I cannot see anything.
But If I change the script in to following with some small changes, it shows me Something Entered as expected.
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('ExampleController', function ($scope) {
$scope.userFirstName = 'Something';
if($scope.userFirstName.length === 0){
$scope.msg = "pls enter something";
}else{
$scope.msg = "Something Entered";
}
});
</script>
What is wrong here? As far as I understand, the first one should work if the second one works as expected. Otherwise, both should not work.
I think I have not understood something very basic. If so, please describe me what I have not read about.
Thank You..!