Paul Samsotha
Paul Samsotha

Reputation: 209102

AngularJS app not running correctly

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.

Upvotes: 2

Views: 18046

Answers (6)

MichaelHasfel
MichaelHasfel

Reputation: 51

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>

Upvotes: 0

D.Evangelista
D.Evangelista

Reputation: 6541

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

Upvotes: 18

Roisgoen
Roisgoen

Reputation: 814

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.

Upvotes: 0

Stackhelper
Stackhelper

Reputation: 63

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.

Upvotes: 0

Sfoorti
Sfoorti

Reputation: 1

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', []);

Upvotes: 0

Prateek
Prateek

Reputation: 6975

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>

Upvotes: 6

Related Questions