(10) Reasons and solutions for the failure of the ng-inlude directive to load the page_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:49:38
Original
1458 people have browsed it

The ng-include directive provided in angularjs is very similar to the 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>
Copy after login

The included child page child.html code is as follows:

 <div>        <h1>included, {{name}}!</h1>        <h1>included, {{age}}!</h1> </div>
Copy after login


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)
Copy after login

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'.
Copy after login


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.


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template