When writing HTML documents, in order to implement the Code modularity, to increase the code readability and maintainability of complex pages, we often think of spreading the code into different HTML files
Angularjs inside the ng-include command combined with Ng-controller can be very convenient to achieve this purpose
The ng-include directive is used to include external HTML files.
ng-include can be used as a property, or an element using the
The demo sample is as follows:
Index.html
<!DOCTYPE HTML><HTMLNg-app= "MYAPP"><Head> <MetaCharSet= "UTF-8"> <title>Demo</title> <!--Angularjs References - <Scripttype= "Text/javascript"src= "Angular/angular.min.js"CharSet= "Utf-8"></Script> <Scripttype= "Text/javascript"src= "SubPage.controller.js" ></Script></Head><BodyNg-controller= "Myctrl"> <DivNg-include= "' subpage.html '"></Div> <!--the path must be enclosed with single quotes -</Body></HTML>
Subpage.html
<DivID= "subpage"> <label>{{title}}<label> <inputNg-model= "value"> <imgsrc= "Eg_tulip.jpg"alt= "Shanghai Flower Harbor-Tulip"style= "width:400px;height:200px" /></Div>
SubPage.controller.js
var app = Angular.module (' myApp ', []); App.controller ("Myctrl", function ($scope) { $scope. title= "type"; $scope. value= "Picture";});
Run index directly on Google Chrome. HTML, error
XMLHttpRequest cannot load file:///D:/learn/include.html.
Cross origin requests is only supported for protocol schemes:http, data, Chrome, chrome-extension, HTTPS
The hint is obvious: you cannot access across domains. With the above error, you can see that the AJAX request is used when using the ng-include instruction XMLHttpRequest
Directives such as Ng-include must be supported by the Web container. Index.html opened directly with the browser and not accessed through the Web container, so there are cross-domain access issues
Workaround:
1. Deploy the code under a Web container such as Tomcat or IIS and access it via HTTP
2. You can use the front-end development artifact Webstorm, which automatically launches the built-in web container when the tool runs HTML
ANGULARJS Study Notes (2)--ng-include