1

I am a beginner to Angular js.I am not sure as to why this error is appearing in my script file. My code looks like this:

angularExample.html

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

app.js

(function(){

var app=angular.module('Tutorial',[]); 

app.directive('tutorExtension',function(){
    return
    {
        restrict: 'E',
        templateUrl: 'tutor-Extension.html',
        controller: function(){
                this.reviews=courseReviews;
                },
        controllerAs: 'extended'
    };
});

var courseReviews=[
{name:'abc',email:'[email protected]'},
{name:'xyz',email:'[email protected]'},
{name:'pqr',email:'[email protected]'}
];


})();

tutor-Extension.html

<div ng-controller="extended as ext">
<ul ng-repeat='review in ext.reviews'>
     <li>{{review.name}}</li>
     <li>{{review.email}}</li>
</ul>
</div>

Error Snapshot:

Error

enter image description here

3 Answers 3

1

There's two mistakes:

  1. you declared controllerAs and redo the same in html template.

You should have tutor-Extension.html as

<div>
  <ul ng-repeat='review in ext.reviews'>
     <li>{{review.name}}</li>
     <li>{{review.email}}</li>
  </ul>
</div>

and in directive you should write

controllerAs: 'ext'
  1. Another issue (syntax error) can be solved by removing new line between return and object declaration. JS engine iterprets your code as if you return nothing and then has a block statement

    app.directive('tutorExtension',function(){ return {

Please find a working sample here http://plnkr.co/edit/YocWRgKtCzlV0SRRpHK9

Sign up to request clarification or add additional context in comments.

4 Comments

It worked on that link but when I ran your code locally, I got this error saying "XMLHttpRequest cannot load file tutor-Extension.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource."
Error message suggests CORS rule violation. Do you have tutor-Extension.html hosted not at the same location as the rest of your code?
I checked and it's a standard problem if opening your html directly, not via web server stackoverflow.com/questions/27742070/…
It worked when I ran this page via web server.Thanks!
1

For routing, you should run your site on a local development server. Read more here

Comments

1

There seems only white space problem.

Your code as below:

(function(){

var app=angular.module('Tutorial',[]); 

app.directive('tutorExtension',function(){
    return
    {
        restrict: 'E',
        templateUrl: 'tutor-Extension.html',
        controller: function(){
                this.reviews=courseReviews;
                },
        controllerAs: 'extended'
    };
});

var courseReviews=[
{name:'abc',email:'[email protected]'},
{name:'xyz',email:'[email protected]'},
{name:'pqr',email:'[email protected]'}
];   

})();

Change it as below: See I remove only the white space from return and opening bracket and next statement :)

(function(){

var app=angular.module('Tutorial',[]); 

app.directive('tutorExtension',function(){
    return {
        restrict: 'E',
        templateUrl: 'tutor-Extension.html',
        controller: function(){
                this.reviews=courseReviews;
                },
        controllerAs: 'extended'
    };
});

var courseReviews=[
{name:'abc',email:'[email protected]'},
{name:'xyz',email:'[email protected]'},
{name:'pqr',email:'[email protected]'}
];

})();

Always remember to blank spaces creates issue so try to avoid it , you can also look into deep here

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.