本文介绍了我们如何使用$编译Angularjs指令外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用 $编译
在控制器中的一个函数里,而不是在一个指令。可能吗?我想下面的code。
I want to use $compile
in a controller inside a function and not in a directive. is it possible? I am trying the below code.
$compile('<div ng-attr-tooltip="test">Cancel</div>')(scope)
但是,这是投掷范围是不确定的错误。我试图通过 $范围
里面的功能,但它不工作。
But this is throwing scope is undefined error. I tried to pass $scope
inside the function but it is not working.
推荐答案
如何将角知道你改变了DOM?您需要追加之前编译您的HTML(使用$编译服务)。
How would Angular know that you changed the DOM? You need to compile your html before appending it (using $compile service).
如果你绝对需要一个指令外部访问你可以创建一个注射器。
If you absolutely need access outside of a directive you can create an injector.
$(function() {
// myApp for test directive to work, ng for $compile
var $injector = angular.injector(['ng', 'myApp']);
$injector.invoke(function($rootScope, $compile) {
$('body').prepend($compile('<div ng-attr-tooltip="test">Cancel</div>')($rootScope));
});
});
这篇关于我们如何使用$编译Angularjs指令外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!