AngularJS angular.isDefined()函数
AngularJS中的angular.isDefined()函数是用来确定isDefined函数中的值是否被定义。如果引用被定义了,它返回true,否则返回false。
语法:
angular.isDefined( value );
参数:
- value:这个参数是一个参考,用来检查输入的值是否被定义。
返回值:如果传递的值是定义的,它返回true,否则返回false。
示例:本示例使用angular.isDefined()函数来确定isDefined函数内的值是否定义。
<!DOCTYPE html>
<html>
<head>
<title>angular.isDefined()</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>
<h3>angular.isDefined()</h3>
<div ng-controller="geek">
<b>Date:</b> {{date}}
<br><br> {{isDefined}}
</div>
<!-- Script to uses angular.isDefined() function -->
<script>
var app = angular.module("app", []);
app.controller('geek', ['scope', function(scope) {
scope.date;
scope.isDefined =
angular.isDefined(scope.date) == true ?
"scope.date is defined." : "$scope.date is undefined.";
}]);
</script>
</body>
</html>
输出:
- 日期未定义。

- 如果定义了Date并且其值是 “2019-04-07T23:46:20.586″。

示例2:本示例通过指定当前时间,描述了AngularJS中**angular.isDefined()函数的用法。
<!DOCTYPE html>
<html>
<head>
<title>angular.isDefined()</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>
<h3>angular.isDefined()</h3>
<div ng-controller="geek">
<b>Time:</b> {{ today| date : 'mediumTime'}}
<br /><br /> {{isDefined}}
</div>
<!-- Script to uses angular.isDefined() function -->
<script>
var app = angular.module('app', []);
app.controller('geek', ['scope',
function(scope) {
scope.today = new Date();
scope.isDefined =
angular.isDefined(scope.today) == true ?
'scope.date is defined.' : '$scope.date is undefined.';
},
]);
</script>
</body>
</html>
输出:
- 时间是没有定义的。

- 当时间被定义。

极客教程