本文介绍了如何从控制器内部访问$ ngModelController没有形式和无指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个新手的错误,但我似乎无法访问的 $ scope.model $ ngModelController 这样我就可以从中攫取 $ viewValue

我(使用UI面罩指令IM)有没有一种形式输入:

<输入类型=文本NG模型=INICIONAME =INICIOUI面罩= 99/99/9999>
//我的控制器内
$ scope.inicio = dateFilter((新日).getTime(),DD / MM / YYYY');

UI-掩模设置$ modelValue超过$ viewValue不同的值,使得它难以格式化的数据发送到服务器。当 $ scope.inicio 模式的转变,该值是没有斜线的日期,如 01012014 。所以,我需要能够得到该输入的控制器,但不必把它包起来的形式,并有能力使用 $ scope.myForm.inicio。$ viewValue 。它必须是可能的...

东西,我知道我可以做,但似乎哈克,必须有一个简单的方法:


  • 把元素的表​​单中,并通过 $ scope.myForm.input访问它。$ viewValue

  • 获取使用jQuery $元素数据(输入[名称=INICIO])的数据('$ ngModelController');

  • 使用Get元素 angular.element(输入[名称=INICIO]')控制器('ngModel');

  • 创建一个指令,把它的输入,并用它更新我的范围模型

app.directive('viewValue',函数(){
  返回{
    优先权:10,
    要求:'ngModel',
    链接:功能(范围,元素,ATTRS,控制器){
      范围。$腕表(attrs.viewValue,功能(为newValue,属性oldValue){
        如果(为newValue!==属性oldValue){
          范围[attrs.viewValue] = $控制器viewValue。
        }
      });
    }
  }
});
<输入类型=文本UI面罩=99/99/9999NG模型=INICIO 视图值=INICIO>


解决方案

我喜欢的指令替代。本质上, UI面罩指令不是做你想要什么,所以你还不如写自己的指令。

您不应该通过 INICIO 你的视图值指令。相反,自己的解析器添加到 ngModelCtrl。$解析器。这里有一个例子:

Maybe it's a rookie mistake, but I can't seem to access the $scope.model's $ngModelController so I can grab the $viewValue from it.

I have an input without a form (im using 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 set the $modelValue a different value than $viewValue, making it hard to send formatted data to the server. When the $scope.inicio model changes, the value is a date without slashes, like 01012014. So I need to be able to get the controller for that input, but without having to wrap it in a form, and have to use $scope.myForm.inicio.$viewValue. It MUST be possible...

Things I know I can do, but seems hacky, there must be a simpler way:

  • Put the element inside a form and access it through $scope.myForm.input.$viewValue
  • Get the element data using jQuery $('input[name="inicio"]').data('$ngModelController');
  • Get the element using angular.element('input[name="inicio"]').controller('ngModel');
  • Create a directive, put it in the input, and update my scope model with it

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">
解决方案

I like the directive alternative. Essentially the ui-mask directive isn't doing what you want, so you might as well write your own directive.

You shouldn't have to pass inicio to your view-value directive. Instead, add your own parser to ngModelCtrl.$parsers. Here's an example: http://stackoverflow.com/a/15556249/215945

这篇关于如何从控制器内部访问$ ngModelController没有形式和无指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 00:51