AngularJS รวม
ด้วย AngularJS คุณสามารถรวม HTML จากไฟล์ภายนอกได้
AngularJS รวม
ด้วย AngularJS คุณสามารถรวมเนื้อหา HTML โดยใช้ คำสั่ง ng-include :
ตัวอย่าง
<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 directive ไม่อนุญาตให้คุณรวมไฟล์จากโดเมนอื่น
หากต้องการรวมไฟล์จากโดเมนอื่น คุณสามารถเพิ่มรายการที่อนุญาตพิเศษของไฟล์ทางกฎหมายและ/หรือโดเมนในฟังก์ชันการกำหนดค่าของแอปพลิเคชันของคุณ:
ตัวอย่าง:
<body ng-app="myApp">
<div ng-include="'https://tryit.w3schools.com/angular_include.php'"></div>
<script>
var app = angular.module('myApp', [])
app.config(function($sceDelegateProvider)
{
$sceDelegateProvider.resourceUrlWhitelist([
'https://tryit.w3schools.com/**'
]);
});
</script>
</body>
ตรวจสอบให้แน่ใจว่าเซิร์ฟเวอร์บนปลายทางอนุญาตการเข้าถึงไฟล์ข้ามโดเมน