【发布时间】:2015-08-13 14:59:09
【问题描述】:
我的指令需要在其模板内的两个部分模板之间进行选择:
<div ng-if="enablerowselection" ui-grid="gridOptions" ui-grid-pagination ui-grid-selection class="grid"></div>
<div ng-if="!enablerowselection" ui-grid="gridOptions" ui-grid-pagination class="grid"></div>
在我的指令的链接器函数中,我从属性中获取 enablerowselection 的值:
function linker(scope, element, attrs) {
scope.enablerowselection = attrs.enablerowselection; //enablerowselection is an attribute for my directive
}
但是,这会导致以下错误:
Error: [$compile:nonassign]
我想我需要编译指令内的部分模板,所以我在链接器函数中添加了以下内容,但它仍然不起作用:
$compile(element.contents())(scope);
编辑:以下是我的指令代码:
angular.module('myApp')
.directive('myDirective', myDirective);
myDirective.$inject = ['$compile'];
function myDirective($compile) {
return {
restrict: 'EA',
scope: {
enablerowselection: '='
},
templateUrl: '/path/to/directive_tmpl.html',
link: linkerFn
};
function linkerFn(scope, element, attrs) {
$scope.enablerowselection = attrs.enablerowselection;
$compile(element.contents())(scope); //does not help
} //linkerFn
}
【问题讨论】:
-
你能添加你的指令代码吗?
-
您可能在指令中使用了隔离范围变量。
-
您可以尝试在隔离范围值中使用
enablerowselection: '?=',方法是使其成为可选 -
@PankajParkar,这似乎不起作用,因为我现在遇到了一个新错误:错误:[$compile:iscp]
标签: javascript angularjs templates angularjs-directive compilation