javascript - AngularJS: How to update view when model changed? -
consider html
form
<form name="form" class="form-horizontal" novalidate> <div class="form-group"> <label class="col-lg-2 control-label">template</label> <div class="col-lg-4"> <label> <select class="form-control" data-ng-model="templateselected" data-ng-options="transactiontemplate.name transactiontemplate in transactiontemplates"> </select> </label> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label" for="transactionname">name</label> <div class="col-lg-4"> <input type="text" name="transactionname" class="form-control" id="transactionname" placeholder="transaction name" data-ng-model="transaction.name" required> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">type</label> <div class="btn-group col-lg-3" data-toggle="buttons" data-ng-model="transaction.debit" required> <label class="btn btn-default" data-ng-click="setdebittransaction('true')"> <input type="radio"> debit </label> <label class="btn btn-default" data-ng-click="setdebittransaction('false')"> <input type="radio"> credit </label> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">date</label> <datepicker data-ng-model="transaction.date"></datepicker> </div> <div class="form-group"> <label class="col-lg-2 control-label">amount</label> <div class="col-lg-4"> <div class="input-append"> <!-- added later --> <!--button type="button" class="btn" ng-model="buttonselect.currency" bs-button-select="['$', '₹']"></button--> <!--suppress htmlforminputwithoutlabel --> <input type="text" name="transactionamount" class="form-control" data-ng-model="transaction.amount" required> </div> </div> </div> <div class="form-group"> <label class="col-lg-2 control-label">category</label> <div class="col-lg-4"> <label> <select data-ng-model="transaction.category" class="form-control" data-ng-options="category.name group category.parent category in categories" required> </select> </label> </div> </div> <div class="form-group"> <div class="controls button-group col-lg-4 col-lg-offset-2"> <button type="submit" class="btn btn-danger" data-ng-disabled="form.$invalid" data-ng-click="savetransaction()"> submit </button> <button type="submit" class="btn btn-link" data-ng-click="resettransactionform()"> cancel </button> </div> </div> </form>
i want update html
elements when model transaction
changes controller
$scope.$watch('templateselected', function () { if ($scope.templateselected !== undefined) { console.log('selected transaction template:', $scope.templateselected); $scope.transaction.name = $scope.templateselected.name; $scope.transaction.debit = $scope.templateselected.debit; $scope.transaction.category = $scope.templateselected.category; } }, true);
all see when templateselected
has valid value , $watch
executed, name
value updated/visible on html
form , not the
$scope.transaction.debit = $scope.templateselected.debit; $scope.transaction.category = $scope.templateselected.category;
what missing here? need display these values well?
updated
created plunkr
Comments
Post a Comment