javascript - Dynamic form Angular issue -
i have written code online dynamic form jsfiddle code
the total , grand total not auto updating. had more simple example before , working single model item, made array , won't work. real program building going have many more fields , trying create pre-example show work. can see dumb thing forgetting?
<div ng-controller="myctrl"> <form name="myform"> <div ng-repeat="item in items track $index"> <input type="text" ng-model="item.a"> <input type="text" ng-model="item.b"> <input type="text" ng-model="item.c"> <label>total: </label><label ng-bind="total(item)"></label> </div> </form> <div> <label>grand total: </label><label ng-bind="grandtotal()"></label> </div> </div> var myapp = angular.module('myapp', []); myapp.controller('myctrl', function($scope) { $scope.items = [ { a: 0, b: 0, c: 0 }, { a: 0, b: 0, c: 0 }]; $scope.total = function(item) { var result = item.a * item.b * item.c; return isnan(result) ? 0 : result; }; $scope.grandtotal = function() { var result = 0; angular.foreach($scope.items, function(item) { result += $scope.total(item); }); return isnan(result) ? "" : result.tostring(); }; });
ng-bind
should without $scope
. don't need mention $scope
in view bindings, directly refers $scope
/this
(context) of controller.
other additionally change input's ng-bind
ng-model
enable 2 way binding on input fields.
markup
<input type="text" ng-model="item.a"> <input type="text" ng-model="item.b"> <input type="text" ng-model="item.c"> ng-bind="total(item)"
Comments
Post a Comment