angularの外でjqueryを呼び出すのはなぜですか? 通常、たとえば角度ディレクティブ内から何かを行い、$compile にアクセスできます。外部へのアクセスが絶対に必要な場合は、インジェクターを作成できます。 (プランカー)
angular.module('myApp', []).directive('test', function($compile) {
return {
restrict: 'E',
link: function(scope, element, attributes) {
$(element).html('<h1>this is a test!</h1>')
}
}
});
///////////////////////////////////////////////////////////////////////////////
// called outside angular, you can create an injector that knows about
// certain modules
///////////////////////////////////////////////////////////////////////////////
$(function() {
// myApp for test directive to work, ng for $compile
var $injector = angular.injector(['ng', 'myApp']);
$injector.invoke(function($rootScope, $compile) {
$('body').prepend($compile('<test>Inside injector</test>')($rootScope));
});
});