ANGULARJS Study Notes (2)--ng-include

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.