AngularJS – isUndefined() 方法


AngularJS 中的 isUndefined() 方法主要用於檢查一個引用是否已經被定義。如果向函式內部傳遞的引用未定義或引用為 undefined,則此方法將返回 True,否則將返回 False。

語法

angular.isUndefined(value)

示例 − 檢查引用是否 undefined

在 Angular 專案目錄中建立一個檔案 "isUndefined.html.3",複製貼上以下程式碼段。

<!DOCTYPE html>
<html>
   <head>
      <title>angular.isUndefined()</title>

      <script src= "https://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">
         Welcome to Tutorials Point
      </h1>
      <h2>AngularJS | angular.isUndefined()</h2>

      <div ng-controller="example">
         <b>Name: {{name}}</b>
         <br><br>
         {{isUndefined}}
         <br><br>
         <b>Name: {{name2}}</b>
         <br><br>
         {{isUndefined1}}
      </div>

      <!-- Script for passing the values and checking... -->
      <script>
         var app = angular.module("app", []);
         app.controller('example',['$scope', function ($scope)
         {
            // Defining the keys & values
            $scope.name = "SIMPLY LEARNING";
            $scope.name2;

            $scope.isUndefined = angular.isUndefined($scope.name) == true
               ? "$scope.name is Undefined."
               : "$scope.name is Defined.";

            $scope.isUndefined1 = angular.isUndefined($scope.name2)== true
               ? "$scope.name2 is Undefined."
               : "$scope.name2 is Defined.";
         }]);
      </script>
   </body>
</html>

輸出

要執行以上程式碼,只需轉到你的檔案並將其作為普通 HTML 檔案執行。你將在瀏覽器視窗中看到以下輸出。

在程式碼中,$scope.name 被定義了,而 $scope.name2 則沒有被定義。因此,我們得到了此輸出。

更新於: 2021 年 10 月 8 日

400 次檢視

開啟您職業生涯

完成課程並獲得認證

開始學習
廣告
© . All rights reserved.