一、AngularJS 包含
在 AngularJS 中,你可以在 HTML 中包含 HTML 文件。
1.1 包含
使用 AngularJS, 你可以使用 ng-include 指令来包含 HTML 内容:
<body ng-app="">
<div ng-include="'test.htm'"></div>
</body>
1.2 包含 AngularJS 代码
ng-include 指令除了可以包含 HTML 文件外,还可以包含 AngularJS 代码:
<!-- 被包含对的代码:sites.html -->
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Url }}</td>
</tr>
</table>
<div ng-app="myApp" ng-controller="sitesCtrl">
<div ng-include="'sites.htm'"></div>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('sitesCtrl', function($scope, $http) {
$http.get("sites.php").then(function (response) {
$scope.names = response.data.records;
});
});
</script>
1.3 跨域包含
默认情况下, ng-include 指令不允许包含其他域名的文件。
如果你需要包含其他域名的文件,你需要设置域名访问白名单:
<body ng-app="myApp">
<div ng-include="'https://c.runoob.com/runoobtest/angular_include.php'"></div>
<script>
var app = angular.module('myApp', [])
app.config(function($sceDelegateProvider) {
// 设置白名单
$sceDelegateProvider.resourceUrlWhitelist([
'https://c.runoob.com/runoobtest/**'
]);
});
</script>
</body>