1

I'm following the browser courses of angularjs that you can find here: https://www.angularjs.org/

My main page is "index.html":

<!DOCTYPE html>
<html ng-app="bookStore">
    <head>
        <link rel="stylesheet" type="text/css" href="bootstrap.min.css"/>
        <script type="text/javascript" src="angular.min.js"></script>
        <script type="text/javascript" src="app.js"></script>
    </head>

    <body ng-controller="StoreController as store">
        <header>
            <h1 class="text-center"> BOOKS OF THE MAGIC LIBRARY </h1>
        </header>

        <div class="list-group">
            <div class="list-group-item" ng-repeat="product in store.products">
                <h2>{{product.title}} <em class="pull-right">{{product.price | currency}}</em></h2>

                <div class="img-wrap">
                    <img src="{{product.image}}"/>
                </div>

                <div ng-include="product-description.html">

                </div>

                <product-decsription></product-description>

            </div>
        </div>

    </body>
</html>

You can see that I tried two times to include a second page, in the code above, but It didn't work. The code in which I try to include a second page is the following (I have tried to use ng-include and the directive also singularly, but I obtained the same result):

            <div ng-include="product-description.html">

            </div>

            <product-decsription></product-description>

The following is the code of app.js:

(function(){
    var app = angular.module('bookStore', []);

    app.controller('StoreController', function(){
        this.products = books;
    });

    app.directive('productDescription', function(){
        return {
            restrict:'E',
            templateUrl: 'product-description.html'
        }
    });

    books = [
        {
            author : "J.K. Rowling",
            title: "The prisoner of Azkaban",
            description: "the story of an innocent prisoner",
            price: 10.50,
            image: "hpa.jpg"
        },
        {
            author: "J.K. Rowling",
            title: "H.P and the Chamber of Secrets",
            description: "the story of a bloody chamber",
            price: 8.00,
            image: "cos.jpg"
        },
        {
            author: "J.K. Rowling",
            title: "H.P and the deathly hollows",
            description: "the story fo deathly hollows",
            price: 15.00,
            image : "dh.jpg"
        }
    ];
})();

The following is the code of "product-description.html":

<h3>Description</h3>
<blockquote>{{product.description}}</blockquote>

I have put all this files (both html ones, both javascript one) in the same folder. Everytime I open the file "index.html" using my browser (google chrome), I can't see the descriptions. The following image shows what I see:

my web-site

I have tried to put a single quote in ng-include inside the double quote, as suggested by dfsq, but it doesn't work (I still have the same result as in the image above):

<!DOCTYPE html>
<html ng-app="bookStore">
    <head>
        <link rel="stylesheet" type="text/css" href="bootstrap.min.css"/>
        <script type="text/javascript" src="angular.min.js"></script>
        <script type="text/javascript" src="app.js"></script>
    </head>

    <body ng-controller="StoreController as store">
        <header>
            <h1 class="text-center"> BOOKS OF THE MAGIC LIBRARY </h1>
        </header>

        <div class="list-group">
            <div class="list-group-item" ng-repeat="product in store.products">
                <h2>{{product.title}} <em class="pull-right">{{product.price | currency}}</em></h2>

                <div class="img-wrap">
                    <img src="{{product.image}}"/>
                </div>

                <div ng-include="'product-description.html'"></div>

            </div>
        </div>

    </body>
</html>

I have found those errors in console running the code above:

errors in console

The problem, as highlighted by comments and replies was that I used "file" as protocol instead of "http" (to do that I should have used a web service). I have installed an IDE which has an integrated web service, so that I have solved the problem. Moroever, there was also another little mistake in the code:

<img src="bla bla"/>

instead of:

<img ng-src="bla bla"/>

I still wait for someone who could tell me why "http-server" didn't work. I will give him the best answer eventually

10
  • what is the error in console that you are getting Commented Sep 27, 2015 at 11:42
  • Why are you having both ng-include and the directive Commented Sep 27, 2015 at 11:43
  • @ShamalPerera to try both. But neither of them work Commented Sep 27, 2015 at 11:44
  • 1
    Looks like your problem is because you're opening the file directly in your browser, looks similar to Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https Commented Sep 27, 2015 at 13:27
  • 1
    Host it in a web server. xampp or whatever Commented Sep 27, 2015 at 13:29

2 Answers 2

2

You need to provide a string path in ngInclude, otherwise it's treated as an expression. Correct code in your case would be (note single quotes in path):

<div ng-include="'product-description.html'"></div>
Sign up to request clarification or add additional context in comments.

4 Comments

I open "Index.html" with google chrome...how can I see errors in console?
I have discovered how to see errors in console. I'm going to publish them editing my question. So see my edits soon
The problem is that you are using file protocol. You need to run webserver to run your angular app. So use http:// protocol instead of file://.
Okay, see my new comment to my question, please
0

The problem, as highlighted by comments and replies was that I used "file" as protocol instead of "http" (to do that I should have used a web service). I have installed an IDE which has an integrated web service, so that I have solved the problem. Moroever, there was also another little mistake in the code:

<img src="bla bla"/>

instead of:

<img ng-src="bla bla"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.