如何获取作为属性传递给指令的未解析 angular 表达式?
How to obtain the non-parsed angular expression passed as attribute to a directive?
这是使用 angular 1.4.8.
我需要将包含 angular 表达式的 HTML 字符串传递给使用其属性的指令。表达式被解析,我只得到结果字符串:
<dynamic-template tpl = "selected: {{selection}}"/>
请参阅下面的 "breakpoint here" 评论。到达那里,我需要 angular 表达式(包含 {{}})而不是结果字符串。
angular.module("sandbox").directive('dynamic-template', [function() {
return {
'replace' : true,
'transclude' : true,
'bindToController' : true,
'compile': function (tElement, tAttrs) {
return {
pre: function preLink(scope, element, attrs, controller){
controller.tpl = attrs.tpl; // breakpoint here
},
post: function (scope, element, attrs, controller){
controller.element = element;
}
}
}
}
}
]);
这能够根据自定义表达式动态插入显示值。
如何实现?我希望在预编译中检查属性值会有所帮助。它仍然提供插值。
在 compileFunction 中完成你的事情,没有任何内插,你得到了选择:{{selection}} 字符串:
angular.module("sandbox").directive('dynamic-template', [function() {
return {
'replace' : true,
'transclude' : true,
'bindToController' : true,
'compile': CompileFunction
};
function CompileFunction(element, attrs) {
// attrs.tpl
return PostLinkFunction;
}
function PostLinkFunction(scope, element, attrs) {
}
}
]);
这是使用 angular 1.4.8.
我需要将包含 angular 表达式的 HTML 字符串传递给使用其属性的指令。表达式被解析,我只得到结果字符串:
<dynamic-template tpl = "selected: {{selection}}"/>
请参阅下面的 "breakpoint here" 评论。到达那里,我需要 angular 表达式(包含 {{}})而不是结果字符串。
angular.module("sandbox").directive('dynamic-template', [function() {
return {
'replace' : true,
'transclude' : true,
'bindToController' : true,
'compile': function (tElement, tAttrs) {
return {
pre: function preLink(scope, element, attrs, controller){
controller.tpl = attrs.tpl; // breakpoint here
},
post: function (scope, element, attrs, controller){
controller.element = element;
}
}
}
}
}
]);
这能够根据自定义表达式动态插入显示值。 如何实现?我希望在预编译中检查属性值会有所帮助。它仍然提供插值。
在 compileFunction 中完成你的事情,没有任何内插,你得到了选择:{{selection}} 字符串:
angular.module("sandbox").directive('dynamic-template', [function() {
return {
'replace' : true,
'transclude' : true,
'bindToController' : true,
'compile': CompileFunction
};
function CompileFunction(element, attrs) {
// attrs.tpl
return PostLinkFunction;
}
function PostLinkFunction(scope, element, attrs) {
}
}
]);