標籤:rip 1.4 訪問 解決方案 其他 example note name repeat
AngularJS 包含
在 AngularJS 中,你可以在 HTML 中包含 HTML 檔案。
使用 AngularJS, 你可以使用 ng-include 指令來包含 HTML 內容:
<body ng-app=""> <div ng-include="‘test.htm‘"></div> </body>
test.htm 檔案代碼:
<h1>菜鳥教程</h1><p>這是一個被包含的 HTML 頁面,使用 ng-include 指令來實現!</p>
ng-include 指令除了可以包含 HTML 檔案外,還可以包含 AngularJS 代碼:
<!DOCTYPE html><html><meta charset="utf-8"><script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script><body><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><p>AngularJS 程式碼封裝含在 "sites.htm" 檔案中。</p></body></html>sites.htm 檔案代碼:
<table><tr ng-repeat="x in names"><td>{{ x.Name }}</td><td>{{ x.Url }}</td></tr></table>跨域包含
預設情況下, ng-include 指令不允許包含其他網域名稱的檔案。
如果你需要包含其他網域名稱的檔案,你需要設定網域名稱訪問白名單:
<!DOCTYPE html><html><meta charset="utf-8"><script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script><body ng-app="myApp"> <div ng-include="‘http://c.runoob.com/runoobtest/angular_include.php‘"></div> <script>var app = angular.module(‘myApp‘, [])app.config(function($sceDelegateProvider) { $sceDelegateProvider.resourceUrlWhitelist([ ‘http://c.runoob.com/runoobtest/**‘ ]);});</script><p>你需要設定服務端允許跨域訪問,設定方法可參考 <a target="_blank" href="/w3cnote/php-ajax-cross-border.html">PHP Ajax 跨域問題最佳解決方案</a>。</body></html>
此外,你還需要設定服務端允許跨域訪問,設定方法可參考:PHP Ajax 跨域問題最佳解決方案。
<?php// 允許所有網域名稱可以訪問header(‘Access-Control-Allow-Origin:*‘); echo ‘<b style="color:red">我是跨域的內容</b>‘;?>
AngularJS學習篇(二十)