Angularjs $viewValue without changing $modelValue -
in angular (1.4.x), there way dynamically change input context without changing $modelvalue? example: possible dynamically toggle time (moment) input text/content local/utc without changing $modelvalue. here's example change both view , model values. need mask input context , not model value.
thanks!
var app = angular.module('testparser', []); app.controller('mainctrl', function($scope) { $scope.data = { name: '' }; }); app.directive('changetime', function() { return { restrict: 'ea', require: 'ngmodel', link: function(scope, element, attrs, ngmodel) { //format text going user (model view) ngmodel.$formatters.push(function(value) { return value; }); //format text user (view model) ngmodel.$parsers.push(function(value) { return value; }); scope.data.time = moment().format('hh:mm:ss') scope.setlocaltime = function() { scope.data.time = moment().local().format('hh:mm:ss'); } scope.setutctime = function() { scope.data.time = moment().utc().format('hh:mm:ss'); } } } });
<html ng-app="testparser"> <head> <meta charset="utf-8" /> <script data-require="angular.js@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script> <script data-require="moment.js@*" data-semver="2.10.2" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.min.js"></script> </head> <body ng-controller="mainctrl"> <input type="button" value="set local" ng-click="setlocaltime()" /> <input type="button" value="set utc" ng-click="setutctime()" /> <input changetime ng-model="data.time" /> <pre>model is: {{data.time}}</pre> </body> </html>
you close.
var app = angular.module('testparser', []); app.controller('mainctrl', function($scope) { $scope.data = { name: '' }; }); app.directive('changetime', function() { return { restrict: 'ea', require: 'ngmodel', link: function(scope, element, attrs, ngmodel) { //format text going user (model view) ngmodel.$formatters.unshift(function(value) { return value; }); //format text user (view model) ngmodel.$parsers.unshift(function(value) { return value; }); scope.data.time = moment().format('hh:mm:ss') scope.setlocaltime = function() { ngmodel.$viewvalue = moment().local().format('hh:mm:ss'); ngmodel.$render(); //scope.data.time = moment().local().format('hh:mm:ss'); } scope.setutctime = function() { ngmodel.$viewvalue = moment().utc().format('hh:mm:ss'); ngmodel.$render(); //scope.data.time = moment().utc().format('hh:mm:ss'); } } } });
Comments
Post a Comment