how can we use $compile outside a directive in Angularjs

前端 未结 5 1942
我在风中等你
我在风中等你 2020-12-03 07:31

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(\'
5条回答
  •  情书的邮戳
    2020-12-03 07:40

    I tried @Vaibhav Jain's answer, with no success. After a little more digging, this is what I found to work on Angular 1.3, and jQuery:

    $(function() {
      angular.element(document).injector().invoke(['$compile', function ($compile) {
        // Create a scope.
        var $scope = angular.element(document.body).scope();
        // Specify what it is we'll be compiling.
        var to_compile = '
    Cancel
    '; // Compile the tag, retrieving the compiled output. var $compiled = $compile(to_compile)($scope); // Ensure the scope and been signalled to digest our data. $scope.$digest(); // Append the compiled output to the page. $compiled.appendTo(document.body); }); });

提交回复
热议问题