AngularJS教程
/ 包含
包含
AngularJS 包含
使用 AngularJS,您可以使用 ng-include
指令包含 HTML 內(nèi)容:
實(shí)例
<body ng-app=""> <div ng-include="'myFile.htm'"></div> </body>運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
包含 AngularJS 代碼
您使用 ng-include 指令包含的 HTML 文件也可以包含 AngularJS 代碼:
myTable.htm:<table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table>
在您的網(wǎng)頁(yè)中包含 "myTable.htm" 文件,所有 AngularJS 代碼都將被執(zhí)行,即使是包含文件中的代碼:
實(shí)例
<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>運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
跨域包括
默認(rèn)情況下,ng-include 指令不允許您包含來(lái)自其他域的文件。
要包含來(lái)自另一個(gè)域的文件,您可以在應(yīng)用程序的 config 函數(shù)中添加合法文件和/或域的白名單:
實(shí)例
<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>運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
請(qǐng)確保目標(biāo)服務(wù)器允許跨域文件訪問(wèn)。