Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度输入格式化程序/解析器指令和插值属性?_Javascript_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 角度输入格式化程序/解析器指令和插值属性?

Javascript 角度输入格式化程序/解析器指令和插值属性?,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我试图编写一个应用于输入元素的可配置指令,该指令需要ngModel,并为ngModel添加了解析器和格式化程序函数 我遇到的问题是,我似乎无法在支持ngModel绑定的同时将插值传递到指令中。例如,我希望能够以以下两种方式之一使用我的指令: 传递文字参数: <input ng-model="foo" my-directive="120" /> 然后在上面的场景中定义并插入scope.myDirective,但现在ngModel被破坏了。我的解析器/格式化程序函数的输入参数是未定义的

我试图编写一个应用于输入元素的可配置指令,该指令需要ngModel,并为ngModel添加了解析器和格式化程序函数

我遇到的问题是,我似乎无法在支持ngModel绑定的同时将插值传递到指令中。例如,我希望能够以以下两种方式之一使用我的指令:

传递文字参数:

<input ng-model="foo" my-directive="120" />
然后在上面的场景中定义并插入scope.myDirective,但现在ngModel被破坏了。我的解析器/格式化程序函数的输入参数是未定义的。发生了什么,我如何实现我想要的行为

指令:

module.directive('myDirective', function () {
        return {
            restrict: 'A',
            require: 'ngModel',
            replace: false,
            link: function (scope, elm, attrs, ctrl) { // attrs.myDirective not interpolated

添加隔离作用域时,您正在创建一个全新的子作用域,该子作用域不继承包含
ngModel
值的作用域。这就是为什么您的解析器和格式化程序没有定义

此外,在您的示例中,要获取
条的值,不需要将其放在大括号中:

<input ng-model='foo' my-directive='bar' />
所以您不需要隔离作用域。只需使用
scope.$eval
计算传递给指令的表达式


.

您可以发布指令的代码吗?我的直觉是,您可能没有使用
require
键,该键将允许您访问
ngModel
控制器。
module.directive('myDirective', function () {
        return {
            restrict: 'A',
            require: 'ngModel',
            replace: false,
            link: function (scope, elm, attrs, ctrl) { // attrs.myDirective not interpolated
<input ng-model='foo' my-directive='bar' />
link: function(scope, element, attr, ctrl) {
  attr.myDirective == 'bar'.
  scope.$eval(attr.myDirective) == // whatever the value of 'bar' is in the current scope
}