2

I'm new to AngularJS. I'm trying to run a simple example from a book, but it's not working correctly, and I can't figure out why.

This code runs fine:

<html ng-app>
<head>
    <script src="angular.js"></script>
    <meta charset="UTF-8">
    <title>Angular </title>
</head>
<body>
    <div ng-controller="HelloController">
        <input ng-model="greeting.text"/>
        <p>{{greeting.text}}, World!</p>
    </div>
    <script src="angular.js"></script>
    <script>
             function HelloController($scope) {
                 $scope.greeting = {text: 'Hello'};
             }
    </script>
</body>
</html>

enter image description here

But this is the code I'm having problems with

<html ng-app='myApp'>
<head>
    <title>Shopping Cart</title>
    <script src="angular.js"></script>
</head>
<body ng-controller='CartController'>
    <h1>Your Order</h1>
    <div ng-repeat="item in items">
        <span>{{item.title}}</span>
        <input ng-model="item.quantity">
        <span>{{item.price| currency}}</span>
        <span>{{item.price * item.quantity| currency}}</span>
        <button ng-click="remove($index)">Remove</button>
    </div>
    <script src="angular.js"></script>
    <script>
                function CartController($scope) {
                    $scope.items = [
                        {title: 'Paint pots', quantity: 8, price: 3.95},
                        {title: 'Polka dots', quantity: 17, price: 12.95},
                        {title: 'Pebbles', quantity: 5, price: 6.95}
                    ];
                    $scope.remove = function(index) {
                        $scope.items.splice(index, 1);
                    };
                }
    </script>
</body>
</html>

Expecting this output:

enter image description here

But getting this output:

enter image description here

I don't understand why I'm not getting the data output, and why it's not repeating. Basically, why the example is not running. I copy and paste the code straight from the book.

4
  • 1
    What are you including angular.js twice? Commented Oct 25, 2013 at 13:17
  • move the controller code out of the controller Commented Oct 25, 2013 at 13:18
  • 1
    Remove the app name from ng-app, or run a .configure statement before your controller. Also, check your console, there's most likely a nice descriptive error there. Commented Oct 25, 2013 at 13:18
  • @Richard Dalton Forgot to remove that from the code. That doesn't fix the problem though. Commented Oct 25, 2013 at 13:22

6 Answers 6

18

When you write ng-app='myApp' you are saying to angular that exists a module named myApp somewhere.

Just add this line before your controllers definition:

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

You can see the docs here and here

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

Comments

6

You should define your myApp module:

<html ng-app='myApp'>
<head>
    <title>Your Shopping Cart</title>
</head>
<body ng-controller='CartController'>
    <h1>Your Order</h1>
    <div ng-repeat='item in items'>
        <span>{{item.title}}</span>
        <input ng-model='item.quantity'>
        <span>{{item.price | currency}}</span>
        <span>{{item.price * item.quantity | currency}}</span>
        <button ng-click="remove($index)">Remove</button>
    </div>
    <script src="lib/angular.js"></script>
    <script>
        var app = angular.module('myApp', []);
        app.controller('CartController', ['$scope', function($scope) {
            $scope.items = [
                {title: 'Paint pots', quantity: 8, price: 3.95},
                {title: 'Polka dots', quantity: 17, price: 12.95},
                {title: 'Pebbles', quantity: 5, price: 6.95}
            ];
            $scope.remove = function(index) {
                $scope.items.splice(index, 1);
            }
        }]);
    </script>
  </body>
</html>

1 Comment

I'm not sure, but including angular.js only once could help, too.
0

Give the name of app as html ng-app="myapp" in html file and then add/define the module into the controller as var myapp = angular.module('myapp', []);

Comments

0

Even I faced this problem, resolved it by invoking module creation scope before controller js . Also ensure module is created in script or js file.

Comments

0

Or you can also add the namespace:

<html lang="en" ng-app="myapp" xmlns:ng="http://angularjs.org">

Even though, adding the module is the best way.

Comments

0

I fixed.

<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>
<script src="angular-1.5.3/angular.js"></script>
<script>
var myApp = angular.module('myApp',[])

myApp.controller('CartController', function($scope) {
            $scope.items = [
            {title: 'Paint pots', quantity: 8, price: 3.95},
            {title: 'Polka Dots', quantity: 17, price: 12.95},
            {title: 'Pebbles', quantity: 5, price: 6.95}
        ];

    $scope.remove = function(index){
        $scope.items.splice(index, 1);
    };
});
</script>   
</head>
<body ng-controller="CartController">
<h1>Your order</h1>

<div ng-repeat="item in items">
    <span>{{item.title}}</span>
    <input ng-model="item.quantity" />
    <span>{{item.price | currency}}</span>
    <span>{{item.price * item.quantity | currency}}</span>
    <button ng-click="remove($index)">Remove</button>
</div>
</body>
</html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.