Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  119] [ 5]  / answers: 1 / hits: 18082  / 11 Years ago, fri, october 25, 2013, 12:00:00

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



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



But getting this output:



enter



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.


More From » angularjs

 Answers
10

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


[#74728] Thursday, October 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
laytonlamontm

Total Points: 745
Total Questions: 130
Total Answers: 130

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
;