javascript - Directive on <input> not working -
i have select element:
<select ng-model="user" data-ng-options="user.forename + ' ' + user.surname user in allusers"></select> and after this, have 3 inputs:
<input type="text" value="{{ user.surname }}" /> <input type="text" value="{{ user.forename }}" /> <input data-convert-json-date data-jsondate="{{ user.dob }}" /> the 3rd input has directive applied convert unix-style date human readable date.
myapp.directive('convertjsondate', function () { homecoming { restrict: 'eac', link: function (scope, el, attrs) { var jsondate = attrs.jsondate; var formatteddate = new date(parseint(jsondate.substr(6))); el.val(formatteddate.format("dd/mm/yyyy")); } } }); however, 3rd input remains blank though first 2 inputs updated select changes.
any ideas why directive isnt working?
it beause not watching changes, not because of input tag...
you need alter code following:
myapp.directive('convertjsondate', function () { homecoming { restrict: 'eac', scope:{ jsondate: '@' }, link: function (scope, el, attrs) { scope.$watch('jsondate', function(jsondate){ var formatteddate = new date(parseint(jsondate.substr(6))); el.val(formatteddate.format("dd/mm/yyyy")); }) } } }); as suggested @ryeballar instead of isolated scope , watch on it, can utilize attrs.$observe(), , more efficient:
attrs.$observe('jsondate', function (jsondate) { var formatteddate = new date(parseint(jsondate.substr(6))); el.val(formatteddate.format("dd/mm/yyyy")); }); javascript angularjs
No comments:
Post a Comment