Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

AngularJS - เมธอด isUndefined()

คำอธิบาย

เมธอด isUndefined() ใน AngularJS ใช้ตรวจสอบว่า Reference (ตัวแปร/คุณสมบัติ) ถูกนิยามหรือไม่ จะคืนค่า True หาก Reference ที่ส่งเข้าไม่ได้ถูกนิยาม (Undefined) มิฉะนั้นคืน False

Syntax

angular.isUndefined(value)

ตัวอย่าง - ตรวจสอบว่า Reference เป็น Undefined หรือไม่

สร้างไฟล์ isUndefined.html ในโฟลเดอร์โปรเจกต์ Angular และวางโค้ดต่อไปนี้:

<!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>
        var app = angular.module("app", []);
        app.controller('example', ['$scope', function ($scope) {
            $scope.name = "SIMPLY LEARNING";
            $scope.name2; // ไม่ได้กำหนดค่า = undefined
            $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>

ผลลัพธ์

เปิดไฟล์ในเบราว์เซอร์ จะเห็น:

  • $scope.name ถูกนิยาม → แสดง "Defined"
  • $scope.name2 ไม่ได้นิยาม → แสดง "Undefined"

หมายเหตุ: AngularJS เป็น Framework เก่า (End of Life) แนะนำให้ใช้ Angular (v2+) หรือ Framework อื่นแทน