0

I am trying to learn Angularjs from a book. However, I typed in the following code from the book and it doesn't work in Chrome:

    <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="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.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>

It just shows the braces. I don't need this solved, i just want to know if this is an old book or something. Does this work for anyone else?

3
  • Where is declared your module "myApp" ? Commented Sep 12, 2015 at 20:24
  • dfsq, I'm sorry i don't understand. isn't the angular script in the bottom of the page? Commented Sep 12, 2015 at 20:29
  • you have to declare an angular app and then add the controller CartController to the app Commented Sep 12, 2015 at 20:30

1 Answer 1

1

You forgot to declare your controller in the module called 'myApp'as your html markup shows it <html ng-app='myApp'>:

angular.module('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);
}
});

Plunkr

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

2 Comments

so is this an old version of the book? that code was pasted directly from it
This book was written before Angular 1.2 obviously.

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.