AngularJS學習篇(二十)

來源:互聯網
上載者:User

標籤: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學習篇(二十)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.