我刚开始学习AngularJS。在我编写的测试程序中,我需要检查我的输入字段是否为空,在控制器(script)中。
以下是我编写的代码
<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>
在这里,我预计输出结果将会是请输入内容
。但实际上没有任何输出。
如果我稍微改变一下脚本,它将按照预期显示已输入内容
。
<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>
这里出了什么问题?据我所知,如果第二个有效,那么第一个应该也有效。否则,两者都无效。
我认为我没有理解某些非常基本的东西。如果是这样,请告诉我我没有阅读到的内容。
谢谢..!