Home >Web Front-end >HTML Tutorial >(10) Reasons and solutions for the failure of the ng-inlude directive to load the page_html/css_WEB-ITnose
The ng-include directive provided in angularjs is very similar to the c70750ddd7b325c1d585dab0ddc131eb in JSP. It is used to merge multiple child pages into the same parent page to avoid the parent page being too large and having poor readability. , difficult to maintain.
The parent page parent.html code is as follows:
<html> <head> <script src="angular-1.2.2/angular.js"></script> <script> function rootController($scope,$rootScope,$injector) { $rootScope.name = "aty"; $rootScope.age = 25; } </script> </head> <body ng-app ng-controller="rootController"> <h1>Hello, {{name}}!</h1> <h1>Hello, {{age}}!</h1> <div id="included" ng-include="'child.html'"> <input type="button" value="2"/> </div> </body></html>
The included child page child.html code is as follows:
<div> <h1>included, {{name}}!</h1> <h1>included, {{age}}!</h1> </div>
I use IE11 and Chrome39 runs parent.html and finds that the child.html page cannot be included in parent.html. The error message reported under IE is as follows:
Error: 拒绝访问。 at Anonymous function (file:///D:/learn/angular-1.2.2/angular.js:7852:7) at sendReq (file:///D:/learn/angular-1.2.2/angular.js:7720:9) at serverRequest (file:///D:/learn/angular-1.2.2/angular.js:7454:9)
The error message reported under chrome is as follows:
XMLHttpRequest cannot load file:///D:/learn/include.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///D:/learn/include.html'.
The prompt under IE is a bit obscure, but the chrome prompt is obvious: cross-domain access is not allowed . From the above error message, you can see that when using the ng-include directive, AJAX request XMLHttpRequest will be used. However, we opened parent.html directly with the browser and did not access it through the web container, so there was a cross-domain access problem, and loading child.html failed. The solution is simple: deploy the code to a web container such as tomcat and access it through http.
When I usually practice javascript or JS framework, I use relatively lightweight tools. I don’t use IDEs like Eclipse. I usually use Notepad writes js code. Notepad can easily call the browser installed on the machine. Instructions like ng-include must be supported by the web container. You can use the front-end development artifact webstorm. When this tool runs HTML, it will automatically start the built-in web container, so that the ng-include instruction will not report an error.