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">
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">
angularjs angularjs-scope angularjs-controller
pocesar
source share