3

Was going through a basic training of AngularJS however get stuck in the first place, with expressions are not evaluating with ng-controller dependency.

index.html

<html ng-app="store">
    <head>
        <title>Angular JS</title>
        <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>
        <h1>{{"Hello"}}</h1>
        <div ng-controller="StoreController as stores">

          <h1> {{stores.products.name}} </h1>
          <h2> {{stores.products.price}} </h2>
          <p> {{stores.products.description}} </p>  
        </div>
    </body>
</html>

app.js

(function () {

    var app = angular.module("store", []);
    app.controller = ('StoreController', function () {
        this.products = gem;
    });
    var gem =
        {
            name:'New Product',
            price:'2.95',
            description: 'This is something you would need to buy!!'
        }
})();

HTML Output

Hello
{{stores.products.name}}
{{stores.products.price}}
{{stores.products.description}}

Please help me what mistake I have done.

2
  • Are you getting any console errors? Commented Aug 25, 2014 at 12:50
  • Sorry which IDE I should use this for? Commented Aug 25, 2014 at 12:54

3 Answers 3

2

You have an = sign in your controller that is incorrect.

(function () {

    var app = angular.module("store", []);
    ////NOT app.controller = ('StoreController'.....
    app.controller('StoreController', function () {
        this.products = gem;
    });
    var gem =
        {
            name:'New Product',
            price:'2.95',
            description: 'This is something you would need to buy!!'
        }
})();
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks oops, dint pay attention. Good eyes. However any IDE I can use?
I use sublime text and I'm trying webstorm right now, both are solid IDEs
But if you check the console log in the browser, hit f12 in chrome for example and you would have seen an error that StoreController is not a function.
You could also define it as StoreController = function () {this.products = gem;}; . Is this in sync with best practices, or not?
@GiannisParaskevopoulos defining the controller function in that fashion makes the controller function available in the global scope (not a good thing). If you are interested in modularizing more, I use browserify take look at my answer here
2

It is more typical and I would say best practice to set any data on $scope. Then you don't have to use the alias for your controller and reference products simply. You would have to inject $scope into your controller. The code would look like:

index.html

<html ng-app="store">
    <head>
        <title>Angular JS</title>
        <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>
        <h1>{{"Hello"}}</h1>
        <div ng-controller="StoreController">

          <h1> {{products.name}} </h1>
          <h2> {{products.price}} </h2>
          <p> {{products.description}} </p>  
        </div>
    </body>
</html>

app.js

(function () {

    var app = angular.module("store", []);
    app.controller('StoreController', ['$scope', function($scope) {
        $scope.products = gem;
    }]);
    var gem =
        {
            name:'New Product',
            price:'2.95',
            description: 'This is something you would need to buy!!'
        }
})();

4 Comments

When working with larger applications there are advantages to using an alias for the controller because it is clear which scope you are working with in the view.
Thanks guys, Let me try it. So my usage with alias is wrong?
This actually won't fix your problem you have a syntax error.
Oh yeah. And I managed to copy that to my answer too. Fixed it. I hope my answer at least removed some confusion with the aliasing which is not necessary in this case.
-2

Pay attention to ng-controller vs ng-repeat.

You want to set the ng-controller attribute to the controller name and in a different attribute ng-repeat for repeating the data.

1 Comment

This doesn't attempt to answer the persons question.

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.