angularjs:如何从ng-include访问指令的范围

时间:2013-09-19 14:47:40

标签: angularjs angularjs-directive angularjs-ng-include

我有一个指令,该模板使用ng-include:

<ng-include src="filterTemplate"></ng-include>

模板由指令动态更改:

link: function (scope, element, attrs) {
                switch (scope.type) {
                    case 'string':
                        scope.filterTemplate = "app/common/views/templates/gridHeaderString.html";
                        break;

                    case 'number':
                        scope.filterTemplate = "app/common/views/templates/gridHeaderNumber.html";
                        break;
                    default:
                }

                scope.filter = function () {
                    scope.filters[scope.col.field] = { value: scope.filterValue, operator: scope.filterOperator };
                    scope.searchMethod();
                }
            }

例如,gridHeaderString模板如下所示:

<input id="test" type="text" placeholder="Filter..." ng-model="filterValue" />

在指令内的filter函数中,scope.filterValue不起作用,因为在ng-include的子范围内存在filterValue。

所以在ng-include模板中,我尝试过:

ng-model="$parent.filterValue" 

但仍然无效。

如何将ng-include中的模型绑定到指令的范围?

1 个答案:

答案 0 :(得分:0)

哼......实际上我觉得那只是我愚蠢而且我没有第一次刷新缓存。

$ parent.filterValue是要走的路。