AngularJS angular.isUndefined()函数
AngularJS中的angular.isUndefined()函数用于确定isUndefined函数中的值是否是未定义的。如果引用是未定义的,它返回真,否则返回假。
语法:
angular.isUndefined( value )
参数值:
- value:它用于引用该值以检查该值是否被定义或未定义。
返回值:如果传递的值是未定义的,它返回真,否则返回假。
实例1:本实例使用AngularJS中的angular.isUndefined()函数来确定isUndefined函数内的值是否未定义。
<!DOCTYPE html>
<html>
<head>
<title>angular.isUndefined() function</title>
<script src=
"//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js">
</script>
</head>
<body ng-app="app" style="text-align:center">
<h1 style="color:green">GeeksforGeeks</h1>
<h2>angular.isUndefined()</h2>
<div ng-controller="isDefinedController">
<b>Date:</b> {{date}}<br><br>
{{isUndefined}}
<br><br><br>
<b>Date1:</b> {{date1}}<br><br>
{{isUndefined1}}
</div>
<script>
var app = angular.module("app", []);
app.controller('isDefinedController',
['scope', function (scope) {
scope.date = new Date;
scope.date1;
scope.isUndefined = angular.isUndefined(scope.date)
== true ? "scope.date is Undefined."
: "scope.date is not Undefined.";
scope.isUndefined1 = angular.isUndefined(scope.date1)
== true ? "scope.date1 is Undefined."
: "scope.date1 is not Undefined.";
}]);
</script>
</body>
</html>
输出:
实例2:本例使用AngularJS中的angular.isUndefined()函数来验证定义和未定义的值。
<!DOCTYPE html>
<html>
<head>
<title>angular.isUndefined() function</title>
<script src=
"http://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js">
</script>
<style>
body {
text-align: center;
font-family: 'Times New Roman', Times, serif;
}
h1 {
color: green;
}
</style>
</head>
<body ng-app="App">
<h1>GeeksforGeeks</h1>
<h3>angular.isUndefined() Function</h3>
<div ng-controller="geek">
First value is {{displayVal1}}<br>
Second value is {{displayVal2}}<br><br>
<input type="button"
ng-click="Check()"
value="Check Value">
</div>
<script>
var App = angular.module("App", []);
App.controller("geek", function (scope) {
var val1 = "GeeksforGeeks";
var val2;
scope.displayVal1 = '';
scope.displayVal2 = '';
scope.Check = function () {
angular.isUndefined(val1)? scope.displayVal1 =
'does not exist':scope.displayVal1 = 'Exist';
angular.isUndefined(val2)? scope.displayVal2 =
'does not exist' :scope.displayVal2 = 'Exist';
}
});
</script>
</body>
</html>
输出: