AngularJS การประกอบ
- หน้าก่อนหน้า AngularJS API
- หน้าต่อไป AngularJS การออนิม่าชั่น
ด้วย AngularJS คุณสามารถรวมไฟล์นอกที่มีรหัส HTML:
AngularJS การประกอบ
ด้วย AngularJS คุณสามารถใช้: ng-include
คำสั่งรวมเนื้อหา HTML:
ตัวอย่าง
<body ng-app=""> <div ng-include="'myFile.htm'"></div> </body>
รวมรหัส AngularJS
ไฟล์ HTML ที่คุณใช้คำสั่ง ng-include รวมเข้ามาก็สามารถรวมรหัส AngularJS ได้:
myTable.htm:<table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table>
รวมไฟล์ "myTable.htm" ในเว็บไซต์ของคุณ และทั้งหมดรหัส AngularJS จะถูกปฎิบัติ แม้ว่าจะเป็นรหัสในไฟล์ที่รวมมาด้วย:
ตัวอย่าง
<body> <div ng-app="myApp" ng-controller="customersCtrl"> <div ng-include="'myTable.htm'"></div> </div> <script> var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) { $http.get("customers.php").then(function (response) { $scope.names = response.data.records; }); }); </script>
รวมเอกสารทางโดเมนที่แยกต่างกัน
โดยมาตรฐาน คำสั่ง ng-include ไม่อนุญาตให้คุณรวมเอกสารจากโดเมนอื่น:
เพื่อรวมเอกสารจากโดเมนอื่น คุณสามารถเพิ่มไฟล์ที่ถูกต้องและ/หรือโดเมนที่ถูกต้องในบล็อคของ config ฟังก์ชันของโปรแกรมของคุณ:
ตัวอย่าง
<body ng-app="myApp"> <div ng-include="'https://tryit.codew3c.com/angular_include.php'"></div> <script> var app = angular.module('myApp', []) app.config(function($sceDelegateProvider) { $sceDelegateProvider.resourceUrlWhitelist([ 'https://tryit.codew3c.com/**' }); }); </script> </body>
กรุณาแน่ใจว่าเซิร์ฟเวอร์เป้าหมายอนุญาตให้เข้าถึงไฟล์ที่อนุญาตเข้าชมข้ามเครือข่าย
- หน้าก่อนหน้า AngularJS API
- หน้าต่อไป AngularJS การออนิม่าชั่น