AngularJS 包含
在 AngularJS 中,你可以在 HTML 中包含 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代码将被执行,甚至包含文件中的代码:
Example
<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 指令不允许包含其他域名的文件。
如果你需要包含其他域名的文件,你需要设置域名访问白名单:
Example:
<body ng-app="myApp">
<div ng-include="'http://www.refsnesdata.no/angular_include.asp'"></div>
<script>
var app = angular.module('myApp', [])
app.config(function($sceDelegateProvider)
{
$sceDelegateProvider.resourceUrlWhitelist([
'http://www.refsnesdata.no/**'
]);
});
</script>
</body>
尝试一下 »
![]() |
确保目的地服务器允许跨域文件访问。 |
---|