0

So, I am learning Angular JS from codeschool course "Shaping up with angular js". The guy on the videos says, that wrapping code in (function(){}) is a good habbit.

BUT, when I do that Im getting an error[$injector:modulerr]`. Without this self-called function syntax everything works fine. And it bothers me, why they tell to do this way, and why does it cause error?

Javid Asgarov
  • 1,511
  • 1
  • 18
  • 32
  • can you show your code? – RahulB Nov 07 '14 at 06:29
  • Think this post will help you http://stackoverflow.com/questions/2421911/what-is-the-purpose-of-wrapping-whole-javascript-files-in-anonymous-functions-li – BotanMan Nov 07 '14 at 06:29
  • @RahulB, sure. `code (function() { var app = angular.module("gemStore", []); var gems = [{ name: "Dodecahedron", price: 2, desc: "some description", canPurchase: true }, { name: "Pentagonal gem", price: 5.95, desc: "...", canPurchase: true } ]; app.controller("StoreController", function () { this.products = gems; }); })` – Javid Asgarov Nov 07 '14 at 06:42

2 Answers2

3

Seems that you are missing the (); self executing braces at the end of your anonymous function:

(function(){
  // all your code
})();
//^^^--------------add this (); at the closing.
Jai
  • 74,255
  • 12
  • 74
  • 103
0

Usually the format should look like this:

angular.module("gemStore", []).controller("StoreController", function () {
    var gems = [
        { name: "Dodecahedron", price: 2, desc: "some description", canPurchase: true },
        { name: "Pentagonal gem", price: 5.95, desc: "...", canPurchase: true }
    ];
    this.products = gems;
})
huan feng
  • 7,307
  • 2
  • 32
  • 56
  • `angular.module("gemStore", [])controller` you missed a `.` dot before `controller`. – Jai Nov 07 '14 at 07:04