How to access $ ngModelController from inside the controller without a form and without a directive

Maybe this is a rookie mistake, but I can't access $scope.model $ngModelController so that I can extract $viewValue from it.

I have an input without a form (im using the ui-mask directive):

 <input type="text" ng-model="inicio" name="inicio" ui-mask="99/99/9999"> 
 // inside my controller $scope.inicio = dateFilter((new Date).getTime(), 'dd/MM/yyyy'); 

ui-mask sets the value of $ modelValue to a different value than $ viewValue, which makes it difficult to send formatted data to the server. When the $scope.inicio model changes, the value is a date without a slash, for example 01012014 . Therefore, I need to get a controller for this input, but without the need to wrap it in a form and use $scope.myForm.inicio.$viewValue . He MUST be possible ...

Things I know I can do, but it seems hacked, there should be an easier way:

  • Place the element inside the form and access it through $scope.myForm.input.$viewValue
  • Get item data using jQuery $('input[name="inicio"]').data('$ngModelController');
  • Get an element using angular.element('input[name="inicio"]').controller('ngModel');
  • Create a directive, put it on a tab and update my area model with
 app.directive('viewValue', function(){ return { priority: 10, require: 'ngModel', link: function(scope, element, attrs, controller){ scope.$watch(attrs.viewValue, function(newValue, oldValue){ if (newValue !== oldValue){ scope[attrs.viewValue] = controller.$viewValue; } }); } } }); 
 <input type="text" ui-mask="99/99/9999" ng-model="inicio" view-value="inicio"> 
+8
angularjs angularjs-scope angularjs-controller
source share
1 answer

I like the alternative directive. Essentially, the ui-mask directive does not do what you want you to write your own directive.

You do not need to pass inicio to your view-value directive. Instead, add your own parser to ngModelCtrl.$parsers . Here is an example: stack overflow

+4
source share

All Articles