AngularJS 包含
使用 AngularJS,您可以包含外部文件中的 HTML。
AngularJS 包含
使用 AngularJS,您可以使用 ng-include 指令包含 HTML 内容
包含 AngularJS 代码
您使用 ng-include 指令包含的 HTML 文件也可以包含 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.w3schools.com/angular_include.php'"></div>
<script>
var app = angular.module('myApp', [])
app.config(function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
'https://tryit.w3schools.com/**'
]);
});
</script>
</body>
自己动手试一试 »
请确保目标服务器允许跨域文件访问。