javascript - Cannot assign read only Angular -
i have controller:
cart.js
var cart = angular.module('tutorialangularapp'); cart.factory('items', function () { var items = {}; items.query = function () { return [ {title: 'paint pots', description: 'pots full of paint', quantity: 8, price: 3.95}, {title: 'polka dots', description: 'dots polka', quantity: 17, price: 2.95}, {title: 'pebbles', description: 'just little rocks', quantity: 5, price: 6.95} ]; }; return items; }); cart.controller('cartctrl', function ($scope, items) { $scope.bill = {}; $scope.discount = {}; $scope.items = items.query(); $scope.totalcart = function () { var total = 0; (var = 0, len = $scope.items.length; < len; i++) { total = total + $scope.items[i].price * $scope.items[i].quantity; } return total; }; $scope.subtotal = function () { return $scope.totalcart() - $scope.discount; }; $scope.calculatediscount = function (newvalue, $scope) { $scope.discount = newvalue > 100 ? 10 : 0; }; $scope.$watch($scope.totalcart, $scope.calculatediscount); });
and trying show data in html view:
<div> <div ng-repeat="item in items"> <span>{{item.title}}</span> <input ng-model="item.quantity"> <span>{{item.price | currency | number: 2}}</span> <p></p> <span>{{item.price * item.quantity | currency | number: 2}}</span> <p></p> </div> <div>total: {{totalcart() | currency}}</div> <p></p> <div>discount: {{discount | currency}}</div> <p></p> <div>subtotal: {{subtotal() | currency}}</div> <p></p> </div>
i getting error
typeerror: cannot assign read property 'discount'
total value calculated fine when each other variable set. not understand why happens since defined correctly. code par of yeoman project. seems know problem?
i believe problem has function $scope.calculatediscount
.
$scope.$watch
takes in 2 parameters, variable , function has oldvalue
, newvalue
(not $scope
). have here info on how utilize $watch
.
Comments
Post a Comment