1

Angular error: Error: Argument 'StoreController as store' is not a function, got undefined

index.html

<!DOCTYPE html>
<!-- runs the module when the document loads -->
<html ng-app='store'>
    <head>
        <link rel='stylesheet' type='text/css' href='http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css'/>
    </head>
    <body>

        <!-- expressions -->
            <!-- directive    controller name    alias -->
        <div ng-controller='StoreController as store'>
            <h1> {{store.product.name}} </h1>
            <h2>${{store.product.price}}</h2>
            <p>{{store.product.description}}</p>
        </div>
        <script type = 'text/javascript' src='https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js'></script>
        <script type ='text/javascript' src='app.js'></script>
    </body>
</html>

app.js

// wrap the function inside a closure.
(function(){
var app = angular.module('store', []);

app.controller('StoreController', function(){
    // will be executed when the store controller is called.
    this.product = gem;
});

var gem = {
    name: 'Dodecahedron',
    price: 2.95,
    description: '. . .'
};

})();

I am brand new to angular and keep getting the error that I posted above in my chrome console whenever I load my app. I think everything is correct. I am not sure why the function StoreController is not defined, am I not passing the controller properly to my index.html?

1 Answer 1

5

From the looks of your code, you're running Angular 1.0.1:

<script type = 'text/javascript' 
src='https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js'></script>

...whereas the Controller as propertyName syntax option wasn't added until 1.1.5. If you'd like to use that syntax, load a newer version of AngularJS. Actually, load a newer version even if you don't need that syntax ;)

https://developers.google.com/speed/libraries/devguide#angularjs

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

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.