该标题是一个满口!我有一些由另一位开发人员编写的代码,并且无法解释他自己。我们没有留在办公室可以弄清楚它为什么有效。任何帮助,将不胜感激!Javascript对象作为Angular指令属性中的函数参数
我们有许多指令将控制器中定义的函数的结果用作属性之一。控制器上的方法看起来像这样。
$scope.required = function(pathString, object){
/*
pathString is a string representation pointing to a property in object
object is
Returns true/false for the property described
in the path string and the state of the object.
*/
}
通常,当我们在html文件中使用它作为指令实现的属性值时,它看起来像这样。
<normal-directive
isrequired='required("Path.to.property", object)'></normal-directive>
奇怪的指令在声明中并不显眼。它的定义与正常指令相同。
directive('weirdDirective', function(){
return {
restrict: 'E',
scope: {
required: '&isrequired'
},
templateUrl: 'path/to/template'
}
});
奇怪的是在执行。这两个新的变量fieldpath和item不会出现在正常指令中传递给函数的字符串和对象。
<weird-directive
isrequired='required(fieldpath, item)'></weird-directive>
变量fieldpath和item不会出现在控制器或怪异指令声明中的任何位置。但是,它们存在于奇怪的指令模板中,如下所示。
<div class='weird-directive'>
<normal-directive
isrequired='required({fieldpath:'Path.to.property', item: object})'></normal-directive>
</div>
这给我提出了各种各样的问题。我认为所需的调用是在父范围内执行的,所以我不明白它是如何使用似乎在模板中赋值的变量。此外,对象通常作为单个参数传递,但它们似乎已经在这里被分离出来以提供所需的两个参数。
我非常想了解这是如何工作的,所以任何帮助或解释将不胜感激!
这种行为很自然,也是有约束力的。 [RTM](https://docs.angularjs.org/api/ng/service/$compile#-scope-)。 – estus