我有一个输入文本字段:
<input type="text" class="form-control" id="inputValue" name="uservalue" ng-model="inputNumber" ng-required="true" autofocus="true" ng-blur="checkIfValid()"/>
我的控制器具有ng-blur功能,如下所示:
$scope.checkIfValid = function(){
console.log("Value is = " + $scope.inputNumber);
}
我已定义 - $scope.inputNumber = '';
我的console.log显示一个空值,即使我在文本字段中输入了一个值,然后点击&#39; Tab&#39;或者转到下一个字段以调用ng-blur功能。
我在这里做错了什么? `
答案 0 :(得分:5)
检查
var app = angular.module("myapp", []);
app.controller("myCtrl", ['$scope', function($scope) {
$scope.inputNumber="new";
$scope.checkIfValid = function(){
console.log("Value is = " + $scope.inputNumber);
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
<div ng-controller="myCtrl">
<input type="text" class="form-control" id="inputValue" name="uservalue" ng-model="inputNumber" ng-required="true" autofocus="true" ng-blur="checkIfValid()"/>
<h1>{{inputNumber}}</h1>
</div>
</div>