Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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
用于通过对象描述的输入的AngularJS指令_Angularjs_Angularjs Directive - Fatal编程技术网

用于通过对象描述的输入的AngularJS指令

用于通过对象描述的输入的AngularJS指令,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我需要能够基于描述输入的对象数组来显示输入。 例如,这描述了两个输入foo和bar: $scope.z.inputs = [{ name: "foo", hint: "Enter foo number.", // if hint is specified, it should be in a tooltip min: "2", // this is optional max: "100" // th

我需要能够基于描述输入的对象数组来显示输入。 例如,这描述了两个输入foo和bar:

$scope.z.inputs = [{
  name: "foo",
  hint: "Enter foo number.",  // if hint is specified, it should be in a tooltip
  min: "2", // this is optional
  max: "100" // this is optional
}, {
  name: "bar"
}];
见PRUNKR: 它有一个处理静态输入的粗糙指令。它无法处理foo和bar的其他输入。指令上方的注释描述了它提供的功能

在保留指令提供的额外功能的同时,可以采取什么方法来处理这些输入

我试过的。。。 在输入元素上设置min={in.min}会导致链接函数中的值为字符串{{in.min}。 我尝试设置范围并使用@see the response at。然而,我不明白如何使用attr.$observe。。。如前所述。而我所做的尝试并不奏效。 另外,在设置指令的作用域后,绑定到具有ng模型的输入的变量不再得到更新

在阅读了中提到的ng开关方法后 我试过ng开关和ng if。我在飞机上放了一个ng if。 如果我可以避免使用ngswitch或ngif来处理诸如min、max和hint之类的东西,这将是非常有用的,因为使用更多属性会很麻烦

使现代化 在plnkr中,我让min和max使用ng if语句处理所有可能的组合。我需要修改min和max指令中的检查。不检查是否指定了数字,只检查是否指定了属性。如果它是一个表达式,那么就不用管它,它会被更新为正确的值


如果有办法的话,我仍然希望能够避免ng ifs的扩散。

我能够找到一个解决方案,避免一堆ng ifs

我使用“最小”和“最大”表达式、数字或未定义来更智能地构建工具提示。我也相应地设置了最小值和最大值

代码的关键部分如下所示:

var min = element.attr('min');
var max = element.attr('max');

// This tooltip can handle if min and/or max are not set, are set to hard coded numbers, or are expressions.
var tooltip = "{{ ("+max+" === undefined)?'Enter an integer greater than or equal to '+("+min+" || 0)+'.':'Enter an integer within the range '+("+min+" || 0)+' and '+"+max+"+'.'}}";

if ( isNaN(min) ) {
   // If an expression was used and it ends up evaluating to be falsey or if min wasn't defined, this will make the min zero.
  element.attr('min', '{{'+min+' || 0}}');
}
if ( isNaN(max) ) {
  // If an expression was used and it ends up evaluating to be falsey or if max wasn't defined, this will set it.
  element.attr('max', '{{'+max+' || 9223372036854770000}}');
}
element.attr('tooltip', tooltip);

八天后见

,仍然没有评论、问题或尝试回答。我在想这个问题是不是还不明白,还是真的那么难解决。