angularjs - Angular directive: How to make assign value to parent scope? -
i have controller appctrl
scope.transaction = {}
the index looks like
<div class="control-group"> <label class="control-label">date</label> <div class="controls"> <div class="control-group input-append date form_datetime"> <date-time-picker data-ng-model="transaction.date"></date-time-picker> </div> </div> </div> <div class="control-group"> <label class="control-label">amount</label> <div class="controls"> <div class="input-append"> <input type="text" name="transactionamount" ng-model="transaction.amount" required> </div>
and custom directive looks like
angular.module('customdirectives', []).directive('datetimepicker', function() { return { restrict: 'e', replace: true, scope: { transaction['date']: '=' # compilation error here }, template: '<div class="control-group input-append date form_datetime">'+ '<input type="text" readonly data-date-format="yyyy-mm-dd hh:ii" name="transactiondate" ng-model="transaction.date" data-date-time required>'+ '<span class="add-on"><em class="icon-remove"></em></span>'+ '<span class="add-on"><em class="icon-th"></em></span>'+ '</div>', link: function(scope, element, attrs, ngmodel) { var input = element.find('input'); element.datetimepicker({ format: "yyyy-mm-ddthh:ii:ssz", showmeridian: true, autoclose: true, todaybtn: true, pickerposition: 'bottom-left' }); element.bind('blur keyup change', function(){ console.log('binding element'); scope.$apply(date); }); function date() { console.log('setting date',input.val()); scope.ngmodel = input.val(); } date(); // initialize } } });
i want assign date value directive $scope.transaction.date
failing compilation error, how can achieve this?
scope: { transaction['date']: '=' # compilation error here },
should
scope: { transactiondate: '=' },
and
<date-time-picker data-ng-model="transaction.date"></date-time-picker>
should be
<date-time-picker transaction-date="transaction.date"></date-time-picker>
then within directive can call scope.transactiondate = myvalue;
within scope.$apply();
edit: if want use ng-model within directive can use
.... restrict: 'e', require: '?ngmodel', ....
and
controller.$setviewvalue(value); //this in directive code want set value of ng-model bound variable.
in html
<date-time-picker data-ng-model="transaction.date"></date-time-picker>
Comments
Post a Comment