AngularJS (18) ng-include 匯入外部HTML
本篇範例的(Plunker Demo)
ng-include用來匯入外部HTML。
網頁隨著開發,會越來越冗長,所以將網頁模組化能增加維護的效率。
方式一,
直接輸入網址字串 'header.html'
<body ng-app="app1"> <div ng-include src="'header.html'"></div> </body>
要注意需要 ' '
方式二,
也可以先在controller定義網址的字串變數。
.controller('MyController', function($scope){ $scope.headerUrl = 'header.html'; });
然後在MyController的範圍內呼叫就可以了。
<div ng-include src="headerUrl"></div>
也可以省略src
<div ng-include="headerUrl"></div>
要注意的是,ng-include官方文件說明ng-include只能插入同網域的網頁
By default, the template URL is restricted to
the same domain and protocol as the application document.
For example, ngInclude won't work for
cross-domain requests on all browsers and for file:// access on some browsers.
留言
張貼留言