How to set a model value from child controller to a model in parent controller in angularjs -
i stuck in application have table containing input fields entering expense_id value , amount. amount element bound custom directive hitting ctrl+enter add new tr containing empty fields.now while submitting unable calculate total amount of amount fields. here markup
<table class="table table-bordered table-hover table-condensed"> <tr class="info" style="font-weight: bold"> <td>expence type</td> <td>amt</td> </tr> <tr ng-repeat="expense in expenceentry"> <td> <!-- editable username (text validation) --> <span editable-text="expense.expensetype_id" ng-model="expense.expensetype_id" e-name="expense.expensetype_id" e-form="expenceentryform" e-required e-typeahead="expense.expensetype_id expense.expensetype_name expense in expensetypes.record"> </span> </td> <td ng-controller="myctrl"> <input type="number" ng-model="expense.amount" on-keyup-fn="handlekeypress"> </td> </tr> </table>
and controller myctrl is:
function myctrl($scope) { $scope.keylog = []; $scope.keycount= 0; var len; //$scope.expense.amount="100"; // $scope.$parent.test=$scope.expense.amount; $scope.handlekeypress = function(key) { $scope.keylog.push(key); if($scope.keylog[len-1]==17 && $scope.keylog[len]==13) $scope.adduser(); len=$scope.keylog.length; } }
custom directive is
app.directive('onkeyup', function() { return function(scope, elm, attrs) { function applykeyup() { scope.$apply(attrs.onkeyup); }; var allowedkeys = scope.$eval(attrs.keys); elm.bind('keyup', function(evt) { //if no key restriction specified, fire if (!allowedkeys || allowedkeys.length == 0) { applykeyup(); } else { angular.foreach(allowedkeys, function(key) { if (key == evt.which) { applykeyup(); } }); } }); };
});
parent controller :
function expenseentry($scope, $filter, $http,expense_type,expenseinsert,employee) { $scope.total="0.00"; $scope.deliveryboys =employee.get(); $scope.update=function(){ // $scope.action="add"; var ret=expenseinsert.save($scope.expense); alert("data inserted..!!!"); }; $scope.expensetypes=expense_type.get(); $scope.adduser = function() { $scope.inserted = { expensetype_id:'', amount: null }; $scope.expenceentry.push($scope.inserted); }; $scope.expenceentry = [ {expensetype_id: '', amount: ''} ];
};
your code quite confusing because not clear information need.
but in case, per title of post. can access immediate parent controller scope using
$scope.$parent.varname;
if not immediate parent , don't use $parent, can rely on inheritance of scopes. child scopes should have access parents scope unless declared isolate scope.just watch out using primitives instead of objects data-binding.
great resource javascript-prototypal-inheritance
Comments
Post a Comment