Reputation: 73
here's my view
md-input-container flex=""
label Mynumber
input type="number" ng-model="mynumber" ng-change="checkMynumber(mynumber)" step="0.01" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" string-to-number=""
my controller
$scope.checkMynumber = (number) =>
if number?
alert "true"
else
alert "false"
when i input my first number it alert True that was right but when i erase my inputed number it keeps alert True. i want to alert false when my input type is null.
Upvotes: 0
Views: 1440
Reputation: 39
I don't understand why almost every answer here is in .js instead .coffee, like in question.. anyway you can imporove your code
ng-model="mynumber" ng-change="checkMynumber(mynumber)"
I guess mynumber
is your scope variable, so why you are passing this to method? (I don't have full code, but you are using fat arrow there, otherwise I would find reason to pass this as argument)
Your method checkMyNumber can be only like this:
$scope.checkMynumber = => alert $scope.mynumber? + ""
Upvotes: 0
Reputation: 73
it because of my directives string-to-number thanks guys its working now
Upvotes: 0
Reputation: 504
$scope.checkMynumber = function (number) {
if(number) {
alert(true)
} else {
alert(false)
}
}
Check this fiddle https://jsfiddle.net/aswathyb950/9jfww52h/
Upvotes: 2
Reputation: 27222
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', ['$scope', function($scope) {
$scope.checkMynumber = function(number) {
number ? alert("true") : alert("false");
}
}]);
<div ng-app="myApp" ng-controller="MyCtrl">
<input type="number" ng-model="mynumber" ng-change="checkMynumber(mynumber)" step="0.01" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" string-to-number="">
</div>
Upvotes: 1
Reputation: 41447
remove the ternary operator and stick with the if else
conditions
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.checkMynumber = function(number) {
if (number)
alert("true")
else
alert ("false")
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<input type="number" ng-model="mynumber" ng-change="checkMynumber(mynumber)" step="0.01" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" string-to-number="">
</div>
Upvotes: 1
Reputation: 821
You can use
$scope.checkMynumber = function(number){ if (number!= null) alert "true" else alert "false" }
Upvotes: 1