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

StackUser
  • 587
  • 6
  • 26
  • what is the error in console that you are getting – joyBlanks Sep 27 '15 at 11:42
  • Why are you having both ng-include and the directive – Shamal Perera Sep 27 '15 at 11:43
  • @ShamalPerera to try both. But neither of them work – StackUser Sep 27 '15 at 11:44
  • I have also tried them individually – StackUser Sep 27 '15 at 11:44
  • @ShamalPerera I think that the more you try, the more you can achieve, so that I have tried them both toghether and I have tried them singularly, but I have obtained the same result in both cases – StackUser Sep 27 '15 at 11:49
  • Your directive won't work cause you spelt description wrong in the opening tag `product-decsription` – logee Sep 27 '15 at 13:11
  • Thank you very much @user2341963, I have corrected it with: . It doesn't work anyway... – StackUser Sep 27 '15 at 13:15
  • 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](http://stackoverflow.com/questions/27742070/angularjs-error-cross-origin-requests-are-only-supported-for-protocol-schemes) – logee Sep 27 '15 at 13:27
  • 1
    Host it in a web server. xampp or whatever – Shamal Perera Sep 27 '15 at 13:29
  • Thank you. I followed the instructions of Kirill Fuchs. I installed nodejs. Then I run "npm install http-server -g". I opened cmd and I went in the folder where there are my html and js files to be run, so I run "http-server index.html". At this point, cmd says: Starting up http-server, serving index.html Available on: 192.168.1.8:8080 etc... If I put in the url: "192.168.1.8:8080"; into the browser it downloades something...if I open it with the browser, I can't see the correct page... – StackUser Sep 27 '15 at 15:18

2 Answers2

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>
dfsq
  • 191,768
  • 25
  • 236
  • 258
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"/>
StackUser
  • 587
  • 6
  • 26