Javascript 如何同时使用继承作用域和隔离作用域
我有需要排序的html表格。Javascript 如何同时使用继承作用域和隔离作用域,javascript,angularjs,Javascript,Angularjs,我有需要排序的html表格。 <table> <thead> <tr> <th custom-sort-one order="'Name'" >Name</th> </tr> </thead> <tbody> <tr> <td>N
<table>
<thead>
<tr>
<th custom-sort-one order="'Name'" >Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name_AAA</td>
</tr>
</tbody>
</table>
指令工作,当用户单击Name
列标题时,名为sort\u by
的函数知道需要排序的列。问题是它不能调用写在控制器中的父作用域函数。
因此,我使用
app.directive(“customSortTwo”,function(){
返回{
限制:“A”,
是的,
///继承范围
范围:正确,
模板:“”,
链接:功能(范围){
scope.sort_by=函数(ColumnToSort){
///ColumnToSort未定义,这不是我想要的
console.log(“ColumnToSort=”,ColumnToSort);
///下面的函数可以执行,因为我更改为继承范围。
TestFunction();
};
}
}
});
最后我可以调用父作用域,但问题是我不能将sort\u by
函数作为参数赋值
您只需稍微修复一下您的问题 此函数有几个参数:
范围
、元素
、属性
等
所以,您可以看到所需属性的值
link: function (scope,elem, attrs) {
scope.order = attrs.order;
当然,这适用于静态属性值,在这种情况下不需要引号。更新:至于第一个函数:您可以从父作用域调用函数,也可以像
<th custom-sort-one order="'Name'" func="TestFunction()" >Name</th>
名称
并可直接使用
&或&attr-提供在父作用域上下文中执行表达式的方法
为什么不呢?您可以获得所需的属性,并查看它,如果字段是静态的,您甚至不需要查看。请尝试
”
谢谢@Grundy提供的完整指导
link: function (scope,elem, attrs) {
scope.order = attrs.order;
<th custom-sort-one order="'Name'" func="TestFunction()" >Name</th>