手册
目录
使用 AngularJS,您可以使用 ng-include 指令包含 HTML 内容:
<body ng-app=""> <div ng-include="'myFile.htm'"></div> </body>运行实例 »
点击 "运行实例" 按钮查看在线实例
您使用 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.php.cn/angular_include.php'"></div>
<script>
var app = angular.module('myApp', [])
app.config(function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
'https://tryit.php.cn/**'
]);
});
</script>
</body>
运行实例 »点击 "运行实例" 按钮查看在线实例
请确保目标服务器允许跨域文件访问。
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.3万人学习
共49课时
77.4万人学习
共29课时
62万人学习
共25课时
39.5万人学习
共43课时
71.3万人学习
共25课时
61.9万人学习
共22课时
23.1万人学习
共28课时
34.1万人学习
共89课时
125.8万人学习